@descope/angular-sdk 0.5.10 → 0.5.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +647 -11
- package/{esm2022 → dist/esm2022}/lib/components/access-key-management/access-key-management.component.mjs +4 -4
- package/{esm2022 → dist/esm2022}/lib/components/audit-management/audit-management.component.mjs +4 -4
- package/dist/esm2022/lib/components/descope/descope.component.mjs +156 -0
- package/{esm2022 → dist/esm2022}/lib/components/role-management/role-management.component.mjs +4 -4
- package/dist/esm2022/lib/components/sign-in-flow/sign-in-flow.component.mjs +44 -0
- package/dist/esm2022/lib/components/sign-up-flow/sign-up-flow.component.mjs +44 -0
- package/dist/esm2022/lib/components/sign-up-or-in-flow/sign-up-or-in-flow.component.mjs +44 -0
- package/{esm2022 → dist/esm2022}/lib/components/user-management/user-management.component.mjs +4 -4
- package/dist/esm2022/lib/components/user-profile/user-profile.component.mjs +66 -0
- package/{esm2022 → dist/esm2022}/lib/descope-auth.module.mjs +5 -5
- package/{esm2022 → dist/esm2022}/lib/services/descope-auth.guard.mjs +1 -1
- package/dist/esm2022/lib/services/descope-auth.service.mjs +154 -0
- package/dist/esm2022/lib/services/descope.interceptor.mjs +51 -0
- package/dist/esm2022/lib/types/types.mjs +6 -0
- package/{esm2022 → dist/esm2022}/lib/utils/constants.mjs +1 -1
- package/dist/esm2022/lib/utils/helpers.mjs +27 -0
- package/{fesm2022 → dist/fesm2022}/descope-angular-sdk.mjs +37 -37
- package/dist/fesm2022/descope-angular-sdk.mjs.map +1 -0
- package/{lib → dist/lib}/services/descope-auth.service.d.ts +3 -2
- package/package.json +72 -25
- package/esm2022/lib/components/descope/descope.component.mjs +0 -156
- package/esm2022/lib/components/sign-in-flow/sign-in-flow.component.mjs +0 -44
- package/esm2022/lib/components/sign-up-flow/sign-up-flow.component.mjs +0 -44
- package/esm2022/lib/components/sign-up-or-in-flow/sign-up-or-in-flow.component.mjs +0 -44
- package/esm2022/lib/components/user-profile/user-profile.component.mjs +0 -66
- package/esm2022/lib/services/descope-auth.service.mjs +0 -154
- package/esm2022/lib/services/descope.interceptor.mjs +0 -51
- package/esm2022/lib/types/types.mjs +0 -6
- package/esm2022/lib/utils/helpers.mjs +0 -27
- package/fesm2022/descope-angular-sdk.mjs.map +0 -1
- /package/{environment.d.ts → dist/environment.d.ts} +0 -0
- /package/{esm2022 → dist/esm2022}/descope-angular-sdk.mjs +0 -0
- /package/{esm2022 → dist/esm2022}/environment.mjs +0 -0
- /package/{esm2022 → dist/esm2022}/public-api.mjs +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{lib → dist/lib}/components/access-key-management/access-key-management.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/audit-management/audit-management.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/descope/descope.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/role-management/role-management.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/sign-in-flow/sign-in-flow.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/sign-up-flow/sign-up-flow.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/sign-up-or-in-flow/sign-up-or-in-flow.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/user-management/user-management.component.d.ts +0 -0
- /package/{lib → dist/lib}/components/user-profile/user-profile.component.d.ts +0 -0
- /package/{lib → dist/lib}/descope-auth.module.d.ts +0 -0
- /package/{lib → dist/lib}/services/descope-auth.guard.d.ts +0 -0
- /package/{lib → dist/lib}/services/descope.interceptor.d.ts +0 -0
- /package/{lib → dist/lib}/types/types.d.ts +0 -0
- /package/{lib → dist/lib}/utils/constants.d.ts +0 -0
- /package/{lib → dist/lib}/utils/helpers.d.ts +0 -0
- /package/{public-api.d.ts → dist/public-api.d.ts} +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Descope <help@descope.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,21 +1,657 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Descope SDK for Angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Descope SDK for Angular provides convenient access to the Descope for an application written on top of Angular. You can read more on the [Descope Website](https://descope.com).
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Requirements
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- The SDK supports Angular version 16 and above.
|
|
8
|
+
- A Descope `Project ID` is required for using the SDK. Find it on the [project page in the Descope Console](https://app.descope.com/settings/project).
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
## Installing the SDK
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
Install the package with:
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
```bash
|
|
15
|
+
npm i --save @descope/angular-sdk
|
|
16
|
+
```
|
|
14
17
|
|
|
15
|
-
|
|
18
|
+
Add Descope type definitions to your `tsconfig.ts`
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
```
|
|
21
|
+
"compilerOptions": {
|
|
22
|
+
"typeRoots": ["./node_modules/@descope"],
|
|
23
|
+
<other options>
|
|
24
|
+
}
|
|
25
|
+
```
|
|
18
26
|
|
|
19
|
-
##
|
|
27
|
+
## Usage
|
|
20
28
|
|
|
21
|
-
|
|
29
|
+
### NgModule - Import `DescopeAuthModule` to your application
|
|
30
|
+
|
|
31
|
+
`app.module.ts`
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { NgModule } from '@angular/core';
|
|
35
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
36
|
+
|
|
37
|
+
import { AppComponent } from './app.component';
|
|
38
|
+
import { DescopeAuthModule } from '@descope/angular-sdk';
|
|
39
|
+
|
|
40
|
+
@NgModule({
|
|
41
|
+
declarations: [AppComponent],
|
|
42
|
+
imports: [
|
|
43
|
+
BrowserModule,
|
|
44
|
+
DescopeAuthModule.forRoot({
|
|
45
|
+
projectId: '<your_project_id>'
|
|
46
|
+
})
|
|
47
|
+
],
|
|
48
|
+
bootstrap: [AppComponent]
|
|
49
|
+
})
|
|
50
|
+
export class AppModule {}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Standalone Mode - Configure Descope SDK for your application
|
|
54
|
+
|
|
55
|
+
`main.ts`
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { bootstrapApplication } from '@angular/platform-browser';
|
|
59
|
+
import { AppComponent } from './app/app.component';
|
|
60
|
+
import { DescopeAuthConfig } from '@descope/angular-sdk';
|
|
61
|
+
|
|
62
|
+
bootstrapApplication(AppComponent, {
|
|
63
|
+
providers: [
|
|
64
|
+
{ provide: DescopeAuthConfig, useValue: { projectId: '<your_project_id>' } }
|
|
65
|
+
]
|
|
66
|
+
}).catch((err) => console.error(err));
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Use Descope to render specific flow
|
|
70
|
+
|
|
71
|
+
You can use **default flows** or **provide flow id** directly to the descope component
|
|
72
|
+
|
|
73
|
+
#### 1. Default flows
|
|
74
|
+
|
|
75
|
+
`app.component.html`
|
|
76
|
+
|
|
77
|
+
```angular2html
|
|
78
|
+
<descope-sign-in-flow
|
|
79
|
+
(success)="onSuccess($event)"
|
|
80
|
+
(error)="onError($event)"
|
|
81
|
+
(ready)="onReady()"
|
|
82
|
+
></descope-sign-in-flow>
|
|
83
|
+
<!-- Optionally, you can show/hide loading indication until the flow page is ready -->
|
|
84
|
+
<div
|
|
85
|
+
*ngIf="isLoading"
|
|
86
|
+
class="loading-indicator"
|
|
87
|
+
style="display: flex; justify-content: center; align-items: center;"
|
|
88
|
+
>
|
|
89
|
+
Loading...
|
|
90
|
+
</div>
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
`app.component.ts`
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { Component } from '@angular/core';
|
|
97
|
+
|
|
98
|
+
@Component({
|
|
99
|
+
selector: 'app-root',
|
|
100
|
+
templateUrl: './app.component.html'
|
|
101
|
+
})
|
|
102
|
+
export class AppComponent {
|
|
103
|
+
// Optionally, you can show/hide loading indication until the flow page is ready
|
|
104
|
+
// See usage in onReady() method and the html template
|
|
105
|
+
isLoading = true;
|
|
106
|
+
|
|
107
|
+
onSuccess(e: CustomEvent) {
|
|
108
|
+
console.log('SUCCESSFULLY LOGGED IN', e.detail);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
onError(e: CustomEvent) {
|
|
112
|
+
console.log('ERROR FROM LOG IN FLOW', e.detail);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
onReady() {
|
|
116
|
+
this.isLoading = false;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 2. Provide flow id
|
|
122
|
+
|
|
123
|
+
```angular2html
|
|
124
|
+
<descope
|
|
125
|
+
flowId="<your_flow_id>"
|
|
126
|
+
(success)="<your_success_function>"
|
|
127
|
+
(error)="<your_error_function>"
|
|
128
|
+
<!-- theme can be "light", "dark" or "os", which auto select a theme based on the OS theme. Default is "light"
|
|
129
|
+
theme="dark"
|
|
130
|
+
|
|
131
|
+
locale can be any supported locale which the flow's screen translated to, if not provided, the locale is taken from the browser's locale.
|
|
132
|
+
locale="en"
|
|
133
|
+
|
|
134
|
+
debug can be set to true to enable debug mode
|
|
135
|
+
debug="true"
|
|
136
|
+
|
|
137
|
+
tenant ID for SSO (SAML) login. If not provided, Descope will use the domain of available email to choose the tenant
|
|
138
|
+
tenant=<tenantId>
|
|
139
|
+
|
|
140
|
+
Redirect URL for OAuth and SSO (will be used when redirecting back from the OAuth provider / IdP), or for "Magic Link" and "Enchanted Link" (will be used as a link in the message sent to the the user)
|
|
141
|
+
redirectUrl=<redirectUrl>
|
|
142
|
+
|
|
143
|
+
telemetryKey=<telemtry_key>
|
|
144
|
+
|
|
145
|
+
autoFocus can be true, false or "skipFirstScreen". Default is true.
|
|
146
|
+
- true: automatically focus on the first input of each screen
|
|
147
|
+
- false: do not automatically focus on screen's inputs
|
|
148
|
+
- "skipFirstScreen": automatically focus on the first input of each screen, except first screen
|
|
149
|
+
autoFocus="skipFirstScreen"
|
|
150
|
+
|
|
151
|
+
errorTransformer is a function that receives an error object and returns a string. The returned string will be displayed to the user.
|
|
152
|
+
NOTE: errorTransformer is not required. If not provided, the error object will be displayed as is.
|
|
153
|
+
Example:
|
|
154
|
+
errorTransformer = (error: { text: string; type: string }): string => {
|
|
155
|
+
const translationMap: { [key: string]: string } = {
|
|
156
|
+
SAMLStartFailed: 'Failed to start SAML flow'
|
|
157
|
+
};
|
|
158
|
+
return translationMap[error.type] || error.text;
|
|
159
|
+
};
|
|
160
|
+
...
|
|
161
|
+
errorTransformer={errorTransformer}
|
|
162
|
+
|
|
163
|
+
form is an object the initial form context that is used in screens inputs in the flow execution.
|
|
164
|
+
Used to inject predefined input values on flow start such as custom inputs, custom attributes and other inputs.
|
|
165
|
+
Keys passed can be accessed in flows actions, conditions and screens prefixed with "form.".
|
|
166
|
+
NOTE: form is not required. If not provided, 'form' context key will be empty before user input.
|
|
167
|
+
Example:
|
|
168
|
+
form={{ email: "predefinedname@domain.com", firstName: "test", "customAttribute.test": "aaaa", "myCustomInput": 12 }}
|
|
169
|
+
|
|
170
|
+
client is an object the initial client context in the flow execution.
|
|
171
|
+
Keys passed can be accessed in flows actions and conditions prefixed with "client.".
|
|
172
|
+
NOTE: client is not required. If not provided, context key will be empty.
|
|
173
|
+
Example:
|
|
174
|
+
client={{ version: "1.2.0" }}
|
|
175
|
+
|
|
176
|
+
logger is an object describing how to log info, warn and errors.
|
|
177
|
+
NOTE: logger is not required. If not provided, the logs will be printed to the console.
|
|
178
|
+
Example:
|
|
179
|
+
const logger = {
|
|
180
|
+
info: (title: string, description: string, state: any) => {
|
|
181
|
+
console.log(title, description, JSON.stringify(state));
|
|
182
|
+
},
|
|
183
|
+
warn: (title: string, description: string) => {
|
|
184
|
+
console.warn(title);
|
|
185
|
+
},
|
|
186
|
+
error: (title: string, description: string) => {
|
|
187
|
+
console.error('OH NOO');
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
...
|
|
191
|
+
logger={logger}-->
|
|
192
|
+
></descope>
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
#### Standalone Mode
|
|
196
|
+
|
|
197
|
+
All components in the sdk are standalone, so you can use them by directly importing them to your components.
|
|
198
|
+
|
|
199
|
+
### Use the `DescopeAuthService` and its exposed fields (`descopeSdk`, `session$`, `user$`) to access authentication state, user details and utilities
|
|
200
|
+
|
|
201
|
+
This can be helpful to implement application-specific logic. Examples:
|
|
202
|
+
|
|
203
|
+
- Render different components if current session is authenticated
|
|
204
|
+
- Render user's content
|
|
205
|
+
- Logout button
|
|
206
|
+
|
|
207
|
+
`app.component.html`
|
|
208
|
+
|
|
209
|
+
```angular2html
|
|
210
|
+
<p *ngIf="!isAuthenticated"> You are not logged in</p>
|
|
211
|
+
<button *ngIf="isAuthenticated" (click)="logout()">LOGOUT</button>
|
|
212
|
+
<p>User: {{userName}}</p>
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`app.component.ts`
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import { Component, OnInit } from '@angular/core';
|
|
219
|
+
import { DescopeAuthService } from '@descope/angular-sdk';
|
|
220
|
+
|
|
221
|
+
@Component({
|
|
222
|
+
selector: 'app-home',
|
|
223
|
+
templateUrl: './app.component.html',
|
|
224
|
+
styleUrls: ['./app.component.scss']
|
|
225
|
+
})
|
|
226
|
+
export class AppComponent implements OnInit {
|
|
227
|
+
isAuthenticated: boolean = false;
|
|
228
|
+
userName: string = '';
|
|
229
|
+
|
|
230
|
+
constructor(private authService: DescopeAuthService) {}
|
|
231
|
+
|
|
232
|
+
ngOnInit() {
|
|
233
|
+
this.authService.session$.subscribe((session) => {
|
|
234
|
+
this.isAuthenticated = session.isAuthenticated;
|
|
235
|
+
});
|
|
236
|
+
this.authService.user$.subscribe((descopeUser) => {
|
|
237
|
+
if (descopeUser.user) {
|
|
238
|
+
this.userName = descopeUser.user.name ?? '';
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
logout() {
|
|
244
|
+
this.authService.descopeSdk.logout();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
### Session Refresh
|
|
250
|
+
|
|
251
|
+
`DescopeAuthService` provides `refreshSession` and `refreshUser` methods that triggers a single request to the Descope backend to attempt to refresh the session or user. You can use them whenever you want to refresh the session/user. For example you can use `APP_INITIALIZER` provider to attempt to refresh session and user on each page refresh:
|
|
252
|
+
|
|
253
|
+
`app.module.ts`
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
import { APP_INITIALIZER, NgModule } from '@angular/core';
|
|
257
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
258
|
+
import { AppComponent } from './app.component';
|
|
259
|
+
import { DescopeAuthModule, DescopeAuthService } from '@descope/angular-sdk';
|
|
260
|
+
import { zip } from 'rxjs';
|
|
261
|
+
|
|
262
|
+
export function initializeApp(authService: DescopeAuthService) {
|
|
263
|
+
return () => zip([authService.refreshSession(), authService.refreshUser()]);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
@NgModule({
|
|
267
|
+
declarations: [AppComponent],
|
|
268
|
+
imports: [
|
|
269
|
+
BrowserModule,
|
|
270
|
+
DescopeAuthModule.forRoot({
|
|
271
|
+
projectId: '<your_project_id>'
|
|
272
|
+
})
|
|
273
|
+
],
|
|
274
|
+
providers: [
|
|
275
|
+
{
|
|
276
|
+
provide: APP_INITIALIZER,
|
|
277
|
+
useFactory: initializeApp,
|
|
278
|
+
deps: [DescopeAuthService],
|
|
279
|
+
multi: true
|
|
280
|
+
}
|
|
281
|
+
],
|
|
282
|
+
bootstrap: [AppComponent]
|
|
283
|
+
})
|
|
284
|
+
export class AppModule {}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
#### Standalone Mode Note:
|
|
288
|
+
|
|
289
|
+
You can use the same approach with `APP_INITIALIZER` in standalone mode, by adding it to `providers` array of the application.
|
|
290
|
+
|
|
291
|
+
### Descope Interceptor
|
|
292
|
+
|
|
293
|
+
You can also use `DescopeInterceptor` to attempt to refresh session on each HTTP request that gets `401` or `403` response:
|
|
294
|
+
|
|
295
|
+
`app.module.ts`
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
import { NgModule } from '@angular/core';
|
|
299
|
+
import { BrowserModule } from '@angular/platform-browser';
|
|
300
|
+
import { AppComponent } from './app.component';
|
|
301
|
+
import {
|
|
302
|
+
HttpClientModule,
|
|
303
|
+
provideHttpClient,
|
|
304
|
+
withInterceptors
|
|
305
|
+
} from '@angular/common/http';
|
|
306
|
+
import { DescopeAuthModule, descopeInterceptor } from '@descope/angular-sdk';
|
|
307
|
+
|
|
308
|
+
@NgModule({
|
|
309
|
+
declarations: [AppComponent],
|
|
310
|
+
imports: [
|
|
311
|
+
BrowserModule,
|
|
312
|
+
HttpClientModule,
|
|
313
|
+
DescopeAuthModule.forRoot({
|
|
314
|
+
projectId: '<your_project_id>',
|
|
315
|
+
pathsToIntercept: ['/protectedPath']
|
|
316
|
+
})
|
|
317
|
+
],
|
|
318
|
+
providers: [provideHttpClient(withInterceptors([descopeInterceptor]))],
|
|
319
|
+
bootstrap: [AppComponent]
|
|
320
|
+
})
|
|
321
|
+
export class AppModule {}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
`DescopeInterceptor`:
|
|
325
|
+
|
|
326
|
+
- is configured for requests that urls contain one of `pathsToIntercept`. If not provided it will be used for all requests.
|
|
327
|
+
- attaches session token as `Authorization` header in `Bearer <token>` format
|
|
328
|
+
- if requests get response with `401` or `403` it automatically attempts to refresh session
|
|
329
|
+
- if refresh attempt is successful, it automatically retries original request, otherwise it fails with original error
|
|
330
|
+
|
|
331
|
+
**For more SDK usage examples refer to [docs](https://docs.descope.com/build/guides/client_sdks/)**
|
|
332
|
+
|
|
333
|
+
### Session token server validation (pass session token to server API)
|
|
334
|
+
|
|
335
|
+
When developing a full-stack application, it is common to have private server API which requires a valid session token:
|
|
336
|
+
|
|
337
|
+

|
|
338
|
+
|
|
339
|
+
Note: Descope also provides server-side SDKs in various languages (NodeJS, Go, Python, etc). Descope's server SDKs have out-of-the-box session validation API that supports the options described bellow. To read more about session validation, Read [this section](https://docs.descope.com/build/guides/gettingstarted/#session-validation) in Descope documentation.
|
|
340
|
+
|
|
341
|
+
You can securely communicate with your backend either by using `DescopeInterceptor` or manually adding token to your requests (ie. by using `DescopeAuthService.getSessionToken()` helper function)
|
|
342
|
+
|
|
343
|
+
### Helper Functions
|
|
344
|
+
|
|
345
|
+
You can also use the following helper methods on `DescopeAuthService` to assist with various actions managing your JWT.
|
|
346
|
+
|
|
347
|
+
- `getSessionToken()` - Get current session token.
|
|
348
|
+
- `getRefreshToken()` - Get current refresh token.
|
|
349
|
+
- `isAuthenticated()` - Returns boolean whether user is authenticated
|
|
350
|
+
- `refreshSession` - Force a refresh on current session token using an existing valid refresh token.
|
|
351
|
+
- `refreshUser` - Force a refresh on current user using an existing valid refresh token.
|
|
352
|
+
- `isSessionTokenExpired(token = getSessionToken())` - Check whether the current session token is expired. Provide a session token if is not persisted.
|
|
353
|
+
- `isRefreshTokenExpired(token = getRefreshToken())` - Check whether the current refresh token is expired. Provide a refresh token if is not persisted.
|
|
354
|
+
- `getJwtRoles(token = getSessionToken(), tenant = '')` - Get current roles from an existing session token. Provide tenant id for specific tenant roles.
|
|
355
|
+
- `getJwtPermissions(token = getSessionToken(), tenant = '')` - Fet current permissions from an existing session token. Provide tenant id for specific tenant permissions.
|
|
356
|
+
|
|
357
|
+
### Refresh token lifecycle
|
|
358
|
+
|
|
359
|
+
Descope SDK is automatically refreshes the session token when it is about to expire. This is done in the background using the refresh token, without any additional configuration.
|
|
360
|
+
|
|
361
|
+
If the Descope project settings are configured to manage tokens in cookies.
|
|
362
|
+
you must also configure a custom domain, and set it as the `baseUrl` in `DescopeAuthModule`.
|
|
363
|
+
|
|
364
|
+
### Descope Guard
|
|
365
|
+
|
|
366
|
+
`angular-sdk` provides a convenient route guard that prevents from accessing given route for users that are not authenticated:
|
|
367
|
+
|
|
368
|
+
```ts
|
|
369
|
+
import { NgModule } from '@angular/core';
|
|
370
|
+
import { RouterModule, Routes } from '@angular/router';
|
|
371
|
+
import { HomeComponent } from './home/home.component';
|
|
372
|
+
import { ProtectedComponent } from './protected/protected.component';
|
|
373
|
+
import { descopeAuthGuard } from '@descope/angular-sdk';
|
|
374
|
+
import { LoginComponent } from './login/login.component';
|
|
375
|
+
|
|
376
|
+
const routes: Routes = [
|
|
377
|
+
{
|
|
378
|
+
path: 'step-up',
|
|
379
|
+
component: ProtectedComponent,
|
|
380
|
+
canActivate: [descopeAuthGuard],
|
|
381
|
+
data: { descopeFallbackUrl: '/' }
|
|
382
|
+
},
|
|
383
|
+
{ path: 'login', component: LoginComponent },
|
|
384
|
+
{ path: '**', component: HomeComponent }
|
|
385
|
+
];
|
|
386
|
+
|
|
387
|
+
@NgModule({
|
|
388
|
+
imports: [RouterModule.forRoot(routes, { enableTracing: false })],
|
|
389
|
+
exports: [RouterModule]
|
|
390
|
+
})
|
|
391
|
+
export class AppRoutingModule {}
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
If not authenticated user tries to access protected route they will be redirected to `descopeFallbackUrl`
|
|
395
|
+
|
|
396
|
+
### Token Persistence
|
|
397
|
+
|
|
398
|
+
Descope stores two tokens: the session token and the refresh token.
|
|
399
|
+
|
|
400
|
+
- The refresh token is either stored in local storage or an `httpOnly` cookie. This is configurable in the Descope console.
|
|
401
|
+
- The session token is stored in either local storage or a JS cookie. This behavior is configurable via the `sessionTokenViaCookie` prop in the `DescopeAuthModule` module.
|
|
402
|
+
|
|
403
|
+
However, for security reasons, you may choose not to store tokens in the browser. In this case, you can pass `persistTokens: false` to the `DescopeAuthModule` module. This prevents the SDK from storing the tokens in the browser.
|
|
404
|
+
|
|
405
|
+
Notes:
|
|
406
|
+
|
|
407
|
+
- You must configure the refresh token to be stored in an `httpOnly` cookie in the Descope console. Otherwise, the refresh token will not be stored, and when the page is refreshed, the user will be logged out.
|
|
408
|
+
- You can still retrieve the session token using the `session` observable of `DescopeAuthService`.
|
|
409
|
+
|
|
410
|
+
### Last User Persistence
|
|
411
|
+
|
|
412
|
+
Descope stores the last user information in local storage. If you wish to disable this feature, you can pass `storeLastAuthenticatedUser: false` to the `DescopeAuthModule` module. Please note that some features related to the last authenticated user may not function as expected if this behavior is disabled.
|
|
413
|
+
|
|
414
|
+
### Widgets
|
|
415
|
+
|
|
416
|
+
Widgets are components that allow you to expose management features for tenant-based implementation. In certain scenarios, your customers may require the capability to perform managerial actions independently, alleviating the necessity to contact you. Widgets serve as a feature enabling you to delegate these capabilities to your customers in a modular manner.
|
|
417
|
+
|
|
418
|
+
Important Note:
|
|
419
|
+
|
|
420
|
+
- For the user to be able to use the widget, they need to be assigned the `Tenant Admin` Role.
|
|
421
|
+
|
|
422
|
+
#### User Management
|
|
423
|
+
|
|
424
|
+
The `UserManagement` widget will let you embed a user table in your site to view and take action.
|
|
425
|
+
|
|
426
|
+
The widget lets you:
|
|
427
|
+
|
|
428
|
+
- Create a new user
|
|
429
|
+
- Edit an existing user
|
|
430
|
+
- Activate / disable an existing user
|
|
431
|
+
- Reset an existing user's password
|
|
432
|
+
- Remove an existing user's passkey
|
|
433
|
+
- Delete an existing user
|
|
434
|
+
|
|
435
|
+
Note:
|
|
436
|
+
|
|
437
|
+
- Custom fields also appear in the table.
|
|
438
|
+
|
|
439
|
+
###### Usage
|
|
440
|
+
|
|
441
|
+
```html
|
|
442
|
+
<user-management tenant="tenant-id" widgetId="user-management-widget" />
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
Example:
|
|
446
|
+
[Manage Users](./projects/demo-app/src/app/manage-users/manage-users.component.html)
|
|
447
|
+
|
|
448
|
+
#### Role Management
|
|
449
|
+
|
|
450
|
+
The `RoleManagement` widget will let you embed a role table in your site to view and take action.
|
|
451
|
+
|
|
452
|
+
The widget lets you:
|
|
453
|
+
|
|
454
|
+
- Create a new role
|
|
455
|
+
- Change an existing role's fields
|
|
456
|
+
- Delete an existing role
|
|
457
|
+
|
|
458
|
+
Note:
|
|
459
|
+
|
|
460
|
+
- The `Editable` field is determined by the user's access to the role - meaning that project-level roles are not editable by tenant level users.
|
|
461
|
+
- You need to pre-define the permissions that the user can use, which are not editable in the widget.
|
|
462
|
+
|
|
463
|
+
###### Usage
|
|
464
|
+
|
|
465
|
+
```html
|
|
466
|
+
<role-management tenant="tenant-id" widgetId="role-management-widget" />
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
Example:
|
|
470
|
+
[Manage Roles](./projects/demo-app/src/app/manage-roles/manage-roles.component.html)
|
|
471
|
+
|
|
472
|
+
#### AccessKeyManagement
|
|
473
|
+
|
|
474
|
+
The `AccessKeyManagement` widget will let you embed an access key table in your site to view and take action.
|
|
475
|
+
|
|
476
|
+
The widget lets you:
|
|
477
|
+
|
|
478
|
+
- Create a new access key
|
|
479
|
+
- Activate / deactivate an existing access key
|
|
480
|
+
- Delete an exising access key
|
|
481
|
+
|
|
482
|
+
###### Usage
|
|
483
|
+
|
|
484
|
+
```html
|
|
485
|
+
<!-- admin view: manage all tenant users' access keys -->
|
|
486
|
+
<access-key-management
|
|
487
|
+
tenant="tenant-id"
|
|
488
|
+
widgetId="access-key-management-widget"
|
|
489
|
+
/>
|
|
490
|
+
|
|
491
|
+
<!-- user view: mange access key for the logged-in tenant's user -->
|
|
492
|
+
<access-key-management
|
|
493
|
+
tenant="tenant-id"
|
|
494
|
+
widgetId="user-access-key-management-widget"
|
|
495
|
+
/>
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
Example:
|
|
499
|
+
[Manage Access Keys](./projects/demo-app/src/app/manage-access-keys/manage-access-keys.component.html)
|
|
500
|
+
|
|
501
|
+
#### AuditManagement
|
|
502
|
+
|
|
503
|
+
The `AuditManagement` widget will let you embed an audit table in your site.
|
|
504
|
+
|
|
505
|
+
###### Usage
|
|
506
|
+
|
|
507
|
+
```html
|
|
508
|
+
<audit-management tenant="tenant-id" widgetId="audit-management-widget" />
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
Example:
|
|
512
|
+
[Manage Audit](./projects/demo-app/src/app/manage-audit/manage-audit.component.html)
|
|
513
|
+
|
|
514
|
+
#### UserProfile
|
|
515
|
+
|
|
516
|
+
The `UserProfile` widget lets you embed a user profile component in your app and let the logged in user update his profile.
|
|
517
|
+
|
|
518
|
+
The widget lets you:
|
|
519
|
+
|
|
520
|
+
- Update user profile picture
|
|
521
|
+
- Update user personal information
|
|
522
|
+
- Update authentication methods
|
|
523
|
+
- Logout
|
|
524
|
+
|
|
525
|
+
###### Usage
|
|
526
|
+
|
|
527
|
+
```angular2html
|
|
528
|
+
<user-profile widgetId="user-profile-widget"
|
|
529
|
+
(logout)="<your_logout_function>"
|
|
530
|
+
/>
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
Example:
|
|
534
|
+
[My User Profile](./projects/demo-app/src/app/my-user-profile/my-user-profile.component.html)
|
|
535
|
+
|
|
536
|
+
## Code Example
|
|
537
|
+
|
|
538
|
+
You can find an example angular app in the [examples folder](./projects/demo-app).
|
|
539
|
+
|
|
540
|
+
### Setup
|
|
541
|
+
|
|
542
|
+
To run the examples, create `environment.development.ts` file in `environments` folder.
|
|
543
|
+
|
|
544
|
+
```ts
|
|
545
|
+
import { Env } from './conifg';
|
|
546
|
+
|
|
547
|
+
export const environment: Env = {
|
|
548
|
+
descopeProjectId: '<your_project_id>'
|
|
549
|
+
};
|
|
550
|
+
```
|
|
551
|
+
|
|
552
|
+
Find your Project ID in the [Descope console](https://app.descope.com/settings/project).
|
|
553
|
+
|
|
554
|
+
### Run Example
|
|
555
|
+
|
|
556
|
+
Run the following command in the root of the project to build and run the example:
|
|
557
|
+
|
|
558
|
+
```bash
|
|
559
|
+
npm i && npm start
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### Example Optional Env Variables
|
|
563
|
+
|
|
564
|
+
See the following table for customization environment variables for the example app:
|
|
565
|
+
|
|
566
|
+
| Env Variable | Description | Default value |
|
|
567
|
+
| -------------------- | ------------------------------------------------------------------------------------------------------------- | ----------------- |
|
|
568
|
+
| descopeFlowId | Which flow ID to use in the login page | **sign-up-or-in** |
|
|
569
|
+
| descopeBaseUrl | Custom Descope base URL | None |
|
|
570
|
+
| descopeBaseStaticUrl | Custom Descope base static URL | None |
|
|
571
|
+
| descopeTheme | Flow theme | None |
|
|
572
|
+
| descopeLocale | Flow locale | Browser's locale |
|
|
573
|
+
| descopeRedirectUrl | Flow redirect URL for OAuth/SSO/Magic Link/Enchanted Link | None |
|
|
574
|
+
| descopeTenantId | Flow tenant ID for SSO/SAML | None |
|
|
575
|
+
| descopeDebugMode | **"true"** - Enable debugger</br>**"false"** - Disable flow debugger | None |
|
|
576
|
+
| descopeStepUpFlowId | Step up flow ID to show to logged in user (via button). e.g. "step-up". Button will be hidden if not provided | None |
|
|
577
|
+
| descopeTelemetryKey | **String** - Telemetry public key provided by Descope Inc | None |
|
|
578
|
+
| descopeBackendUrl | Url to your test backend app in case you want to test e2e | None |
|
|
579
|
+
|
|
580
|
+
Example `environment.development.ts` file:
|
|
581
|
+
|
|
582
|
+
```ts
|
|
583
|
+
import { Env } from './conifg';
|
|
584
|
+
|
|
585
|
+
export const environment: Env = {
|
|
586
|
+
descopeProjectId: '<your_project_id>',
|
|
587
|
+
descopeBaseUrl: '<your_base_url>',
|
|
588
|
+
descopeBaseStaticUrl: '<your_base_static_url>',
|
|
589
|
+
descopeFlowId: 'sign-in',
|
|
590
|
+
descopeDebugMode: false,
|
|
591
|
+
descopeTheme: 'os',
|
|
592
|
+
descopeLocale: 'en_US',
|
|
593
|
+
descopeRedirectUrl: '<your_redirect_url>',
|
|
594
|
+
descopeTelemetryKey: '<your_telemetry_key>',
|
|
595
|
+
descopeStepUpFlowId: 'step-up',
|
|
596
|
+
descopeBackendUrl: 'http://localhost:8080/protected'
|
|
597
|
+
};
|
|
598
|
+
```
|
|
599
|
+
|
|
600
|
+
## Troubleshooting
|
|
601
|
+
|
|
602
|
+
If you encounter warning during build of your application:
|
|
603
|
+
|
|
604
|
+
```
|
|
605
|
+
▲ [WARNING] Module 'lodash.get' used by 'node_modules/@descope/web-component/node_modules/@descope/core-js-sdk/dist/index.esm.js' is not ESM
|
|
606
|
+
```
|
|
607
|
+
|
|
608
|
+
add `lodash.get` to allowed CommonJS dependencies in `angular.json`
|
|
609
|
+
|
|
610
|
+
```json
|
|
611
|
+
"architect": {
|
|
612
|
+
"build": {
|
|
613
|
+
"builder": "@angular-devkit/build-angular:browser",
|
|
614
|
+
"options": {
|
|
615
|
+
"allowedCommonJsDependencies": ["lodash.get"],
|
|
616
|
+
<other_options>
|
|
617
|
+
}
|
|
618
|
+
<other_config>
|
|
619
|
+
}
|
|
620
|
+
<other_config>
|
|
621
|
+
}
|
|
622
|
+
```
|
|
623
|
+
|
|
624
|
+
## FAQ
|
|
625
|
+
|
|
626
|
+
### I updated the user in my backend, but the user / session token are not updated in the frontend
|
|
627
|
+
|
|
628
|
+
The Descope SDK caches the user and session token in the frontend. If you update the user in your backend (using Descope Management SDK/API for example), you can call `me` / `refresh` from `descopeSdk` member of `DescopeAuthService` to refresh the user and session token. Example:
|
|
629
|
+
|
|
630
|
+
```ts
|
|
631
|
+
import { DescopeAuthService } from '@descope/angular-sdk';
|
|
632
|
+
|
|
633
|
+
export class MyComponent {
|
|
634
|
+
// ...
|
|
635
|
+
constructor(private authService: DescopeAuthService) {}
|
|
636
|
+
|
|
637
|
+
handleUpdateUser() {
|
|
638
|
+
myBackendUpdateUser().then(() => {
|
|
639
|
+
this.authService.descopeSdk.me();
|
|
640
|
+
// or
|
|
641
|
+
this.authService.descopeSdk.refresh();
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
## Learn More
|
|
648
|
+
|
|
649
|
+
To learn more please see the [Descope Documentation and API reference page](https://docs.descope.com/).
|
|
650
|
+
|
|
651
|
+
## Contact Us
|
|
652
|
+
|
|
653
|
+
If you need help you can email [Descope Support](mailto:support@descope.com)
|
|
654
|
+
|
|
655
|
+
## License
|
|
656
|
+
|
|
657
|
+
The Descope SDK for Angular is licensed for use under the terms and conditions of the [MIT license Agreement](./LICENSE).
|