@auth0/auth0-angular 1.10.1 → 1.11.1
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/README.md +86 -494
- package/auth0-auth0-angular.d.ts +1 -1
- package/bundles/auth0-auth0-angular.umd.js +103 -90
- package/bundles/auth0-auth0-angular.umd.js.map +1 -1
- package/esm2015/auth0-auth0-angular.js +1 -2
- package/esm2015/lib/abstract-navigator.js +10 -13
- package/esm2015/lib/auth.config.js +12 -8
- package/esm2015/lib/auth.guard.js +9 -11
- package/esm2015/lib/auth.interceptor.js +14 -12
- package/esm2015/lib/auth.module.js +8 -4
- package/esm2015/lib/auth.service.js +16 -21
- package/esm2015/lib/auth.state.js +11 -10
- package/esm2015/useragent.js +2 -2
- package/fesm2015/auth0-auth0-angular.js +89 -84
- package/fesm2015/auth0-auth0-angular.js.map +1 -1
- package/lib/abstract-navigator.d.ts +3 -0
- package/lib/auth.config.d.ts +3 -0
- package/lib/auth.guard.d.ts +3 -0
- package/lib/auth.interceptor.d.ts +3 -0
- package/lib/auth.module.d.ts +4 -0
- package/lib/auth.service.d.ts +3 -0
- package/lib/auth.state.d.ts +3 -0
- package/package.json +4 -5
- package/auth0-auth0-angular.metadata.json +0 -1
package/README.md
CHANGED
|
@@ -1,32 +1,30 @@
|
|
|
1
|
-

|
|
2
|
+
|
|
3
|
+
A library for integrating [Auth0](https://auth0.com) into an Angular application.
|
|
4
|
+
|
|
5
|
+

|
|
4
6
|
[](https://codecov.io/gh/auth0/auth0-angular)
|
|
5
7
|

|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
[](https://circleci.com/gh/auth0/auth0-angular)
|
|
6
10
|
|
|
7
|
-
#
|
|
11
|
+
📚 [Documentation](#documentation) - 🚀 [Getting Started](#getting-started) - 💻 [API Reference](#api-reference) - 💬 [Feedback](#feedback)
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
## Documentation
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
- [Quickstart](https://auth0.com/docs/quickstart/spa/angular) - our interactive guide for quickly adding login, logout and user information to an Angular app using Auth0.
|
|
16
|
+
- [Sample App](https://github.com/auth0-samples/auth0-angular-samples/tree/master/Sample-01) - a full-fledged Angular application integrated with Auth0.
|
|
17
|
+
- [FAQs](https://github.com/auth0/auth0-angular/tree/master/FAQ.md) - frequently asked questions about the auth0-angular SDK.
|
|
18
|
+
- [Examples](https://github.com/auth0/auth0-angular/tree/master/EXAMPLES.md) - code samples for common Angular authentication scenario's.
|
|
19
|
+
- [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0.
|
|
12
20
|
|
|
13
|
-
|
|
14
|
-
- [Installation](#installation)
|
|
15
|
-
- [Getting Started](#getting-started)
|
|
16
|
-
- [Angular Universal](#angular-universal)
|
|
17
|
-
- [Development](#development)
|
|
18
|
-
- [Contributing](#contributing)
|
|
19
|
-
- [Support + Feedback](#support--feedback)
|
|
20
|
-
- [Vulnerability Reporting](#vulnerability-reporting)
|
|
21
|
-
- [What is Auth0](#what-is-auth0)
|
|
22
|
-
- [License](#license)
|
|
21
|
+
## Getting started
|
|
23
22
|
|
|
24
|
-
|
|
23
|
+
### Requirements
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
- [Quickstart Guide](https://auth0.com/docs/quickstart/spa/angular-next)
|
|
25
|
+
This project only supports the [actively supported versions of Angular as stated in the Angular documentation](https://angular.io/guide/releases#actively-supported-versions). Whilst other versions might be compatible they are not actively supported.
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
### Installation
|
|
30
28
|
|
|
31
29
|
Using npm:
|
|
32
30
|
|
|
@@ -40,363 +38,52 @@ We also have `ng-add` support, so the library can also be installed using the An
|
|
|
40
38
|
ng add @auth0/auth0-angular
|
|
41
39
|
```
|
|
42
40
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
- [Register the authentication module](#register-the-authentication-module)
|
|
46
|
-
- [Add login to your application](#add-login-to-your-application)
|
|
47
|
-
- [Add logout to your application](#add-logout-to-your-application)
|
|
48
|
-
- [Display the user profile](#display-the-user-profile)
|
|
49
|
-
- [Protect a route](#protect-a-route)
|
|
50
|
-
- [Call an API](#call-an-api)
|
|
51
|
-
- [Dynamic configuration](#dynamic-configuration)
|
|
52
|
-
- [Using multiple OAuth providers](#using-multiple-oauth-providers)
|
|
53
|
-
|
|
54
|
-
### Register the authentication module
|
|
55
|
-
|
|
56
|
-
Install the SDK into your application by importing `AuthModule` and configuring with your Auth0 domain and client ID:
|
|
57
|
-
|
|
58
|
-
```js
|
|
59
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
60
|
-
import { NgModule } from '@angular/core';
|
|
61
|
-
import { AppComponent } from './app.component';
|
|
62
|
-
|
|
63
|
-
// Import the module from the SDK
|
|
64
|
-
import { AuthModule } from '@auth0/auth0-angular';
|
|
65
|
-
|
|
66
|
-
@NgModule({
|
|
67
|
-
declarations: [AppComponent],
|
|
68
|
-
imports: [
|
|
69
|
-
BrowserModule,
|
|
70
|
-
|
|
71
|
-
// Import the module into the application, with configuration
|
|
72
|
-
AuthModule.forRoot({
|
|
73
|
-
domain: 'YOUR_AUTH0_DOMAIN',
|
|
74
|
-
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
75
|
-
}),
|
|
76
|
-
],
|
|
77
|
-
|
|
78
|
-
bootstrap: [AppComponent],
|
|
79
|
-
})
|
|
80
|
-
export class AppModule {}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
### Add login to your application
|
|
84
|
-
|
|
85
|
-
Next, inject the `AuthService` service into a component where you intend to provide the functionality to log in, by adding the `AuthService` type to your constructor. Then, provide a `loginWithRedirect()` method and call `this.auth.loginWithRedirect()` to log the user into the application.
|
|
86
|
-
|
|
87
|
-
```js
|
|
88
|
-
import { Component } from '@angular/core';
|
|
89
|
-
|
|
90
|
-
// Import the AuthService type from the SDK
|
|
91
|
-
import { AuthService } from '@auth0/auth0-angular';
|
|
92
|
-
|
|
93
|
-
@Component({
|
|
94
|
-
selector: 'app-root',
|
|
95
|
-
templateUrl: './app.component.html',
|
|
96
|
-
styleUrls: ['./app.component.css'],
|
|
97
|
-
})
|
|
98
|
-
export class AppComponent {
|
|
99
|
-
title = 'My App';
|
|
100
|
-
|
|
101
|
-
// Inject the authentication service into your component through the constructor
|
|
102
|
-
constructor(public auth: AuthService) {}
|
|
103
|
-
|
|
104
|
-
loginWithRedirect(): void {
|
|
105
|
-
// Call this to redirect the user to the login page
|
|
106
|
-
this.auth.loginWithRedirect();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
By default the application will ask Auth0 will redirect back to the root URL of your application after authentication, but this can be configured by setting the [`redirectUri` option](https://auth0.github.io/auth0-angular/interfaces/authconfig.html#redirecturi).
|
|
41
|
+
### Configure Auth0
|
|
112
42
|
|
|
113
|
-
|
|
43
|
+
Create a **Single Page Application** in the [Auth0 Dashboard](https://manage.auth0.com/#/applications).
|
|
114
44
|
|
|
115
|
-
|
|
116
|
-
<button
|
|
117
|
-
*ngIf="(auth.isAuthenticated$ | async) === false"
|
|
118
|
-
(click)="loginWithRedirect()"
|
|
45
|
+
> **If you're using an existing application**, verify that you have configured the following settings in your Single Page Application:
|
|
119
46
|
>
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
Add a `logout` method to your component and call the SDK's `logout` method:
|
|
127
|
-
|
|
128
|
-
```js
|
|
129
|
-
logout(): void {
|
|
130
|
-
// Call this to log the user out of the application
|
|
131
|
-
this.auth.logout({ returnTo: window.location.origin });
|
|
132
|
-
}
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
Then on your component's template, add a button that will log the user out of the application. Use the `isAuthenticated$` observable to check that the user has already been authenticated:
|
|
136
|
-
|
|
137
|
-
```html
|
|
138
|
-
<button *ngIf="auth.isAuthenticated$ | async" (click)="logout()">
|
|
139
|
-
Log out
|
|
140
|
-
</button>
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### Display the user profile
|
|
144
|
-
|
|
145
|
-
Access the `user$` observable on the `AuthService` instance to retrieve the user profile. This observable already heeds the `isAuthenticated$` observable, so you do not need to check if the user is authenticated before using it:
|
|
146
|
-
|
|
147
|
-
```html
|
|
148
|
-
<ul *ngIf="auth.user$ | async as user">
|
|
149
|
-
<li>{{ user.name }}</li>
|
|
150
|
-
<li>{{ user.email }}</li>
|
|
151
|
-
</ul>
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Access ID token claims
|
|
47
|
+
> - Click on the "Settings" tab of your application's page.
|
|
48
|
+
> - Ensure that "Token Endpoint Authentication Method" under "Application Properties" is set to "None"
|
|
49
|
+
> - Scroll down and click on the "Show Advanced Settings" link.
|
|
50
|
+
> - Under "Advanced Settings", click on the "OAuth" tab.
|
|
51
|
+
> - Ensure that "JsonWebToken Signature Algorithm" is set to `RS256` and that "OIDC Conformant" is enabled.
|
|
155
52
|
|
|
156
|
-
|
|
53
|
+
Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:
|
|
157
54
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
### Handle errors
|
|
55
|
+
- **Allowed Callback URLs**: `http://localhost:4200`
|
|
56
|
+
- **Allowed Logout URLs**: `http://localhost:4200`
|
|
57
|
+
- **Allowed Web Origins**: `http://localhost:4200`
|
|
163
58
|
|
|
164
|
-
|
|
59
|
+
> These URLs should reflect the origins that your application is running on. **Allowed Callback URLs** may also include a path, depending on where you're handling the callback.
|
|
165
60
|
|
|
166
|
-
|
|
167
|
-
authService.error$.subscribe((error) => console.log(error));
|
|
168
|
-
```
|
|
61
|
+
Take note of the **Client ID** and **Domain** values under the "Basic Information" section. You'll need these values in the next step.
|
|
169
62
|
|
|
170
|
-
###
|
|
63
|
+
### Configure the SDK
|
|
171
64
|
|
|
172
|
-
|
|
65
|
+
#### Static configuration
|
|
173
66
|
|
|
174
|
-
|
|
67
|
+
Install the SDK into your application by importing `AuthModule.forRoot()` and configuring with your Auth0 domain and client id:
|
|
175
68
|
|
|
176
|
-
```
|
|
69
|
+
```ts
|
|
177
70
|
import { NgModule } from '@angular/core';
|
|
178
|
-
import { Routes, RouterModule } from '@angular/router';
|
|
179
|
-
import { HomeComponent } from './unprotected/unprotected.component';
|
|
180
|
-
import { ProtectedComponent } from './protected/protected.component';
|
|
181
|
-
|
|
182
|
-
// Import the authentication guard
|
|
183
|
-
import { AuthGuard } from '@auth0/auth0-angular';
|
|
184
|
-
|
|
185
|
-
const routes: Routes = [
|
|
186
|
-
{
|
|
187
|
-
path: 'protected',
|
|
188
|
-
component: ProtectedComponent,
|
|
189
|
-
|
|
190
|
-
// Protect a route by registering the auth guard in the `canActivate` hook
|
|
191
|
-
canActivate: [AuthGuard],
|
|
192
|
-
},
|
|
193
|
-
{
|
|
194
|
-
path: '',
|
|
195
|
-
component: HomeComponent,
|
|
196
|
-
pathMatch: 'full',
|
|
197
|
-
},
|
|
198
|
-
];
|
|
199
|
-
|
|
200
|
-
@NgModule({
|
|
201
|
-
imports: [RouterModule.forRoot(routes)],
|
|
202
|
-
exports: [RouterModule],
|
|
203
|
-
})
|
|
204
|
-
export class AppRoutingModule {}
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### Call an API
|
|
208
|
-
|
|
209
|
-
The SDK provides an `HttpInterceptor` that automatically attaches access tokens to outgoing requests when using the built-in `HttpClient`. However, you must provide configuration that tells the interceptor which requests to attach access tokens to.
|
|
210
|
-
|
|
211
|
-
#### Specify the Audience
|
|
212
|
-
In order for Auth0 to be able to issue tokens for a specific API, we need to configure the Audience to inform Auth0 about the API in question.
|
|
213
|
-
Set the `audience`, when calling `AuthModule.forRoot()`, to the **API Identifier** of the API from within your Auth0 dashboard.
|
|
214
|
-
|
|
215
|
-
```js
|
|
216
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
217
|
-
import { NgModule } from '@angular/core';
|
|
218
|
-
import { AppComponent } from './app.component';
|
|
219
|
-
|
|
220
|
-
// Import the module from the SDK
|
|
221
71
|
import { AuthModule } from '@auth0/auth0-angular';
|
|
222
72
|
|
|
223
73
|
@NgModule({
|
|
224
|
-
|
|
74
|
+
// ...
|
|
225
75
|
imports: [
|
|
226
|
-
BrowserModule,
|
|
227
|
-
|
|
228
|
-
// Import the module into the application, with configuration
|
|
229
76
|
AuthModule.forRoot({
|
|
230
77
|
domain: 'YOUR_AUTH0_DOMAIN',
|
|
231
78
|
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
232
|
-
audience: 'YOUR_AUTH0_API_IDENTIFIER',
|
|
233
79
|
}),
|
|
234
80
|
],
|
|
235
|
-
|
|
236
|
-
bootstrap: [AppComponent],
|
|
81
|
+
// ...
|
|
237
82
|
})
|
|
238
83
|
export class AppModule {}
|
|
239
84
|
```
|
|
240
85
|
|
|
241
|
-
####
|
|
242
|
-
|
|
243
|
-
First, register the interceptor with your application module, along with the `HttpClientModule`.
|
|
244
|
-
|
|
245
|
-
**Note:** We do not do this automatically for you as we want you to be explicit about including this interceptor. Also, you may want to chain this interceptor with others, making it hard for us to place it accurately.
|
|
246
|
-
|
|
247
|
-
```js
|
|
248
|
-
// Import the interceptor module and the Angular types you'll need
|
|
249
|
-
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
|
250
|
-
import { AuthHttpInterceptor } from '@auth0/auth0-angular';
|
|
251
|
-
|
|
252
|
-
// Register the interceptor with your app module in the `providers` array
|
|
253
|
-
@NgModule({
|
|
254
|
-
declarations: [],
|
|
255
|
-
imports: [
|
|
256
|
-
BrowserModule,
|
|
257
|
-
HttpClientModule, // Register this so that you can make API calls using HttpClient
|
|
258
|
-
AppRoutingModule,
|
|
259
|
-
AuthModule.forRoot(...),
|
|
260
|
-
],
|
|
261
|
-
providers: [
|
|
262
|
-
{ provide: HTTP_INTERCEPTORS, useClass: AuthHttpInterceptor, multi: true },
|
|
263
|
-
],
|
|
264
|
-
bootstrap: [AppComponent],
|
|
265
|
-
})
|
|
266
|
-
```
|
|
267
|
-
|
|
268
|
-
#### Configure AuthHttpInterceptor to attach access tokens
|
|
269
|
-
|
|
270
|
-
Next, tell the SDK which requests to attach access tokens to in the SDK configuration. These are matched on the URL by using a string, a regex, or more complex object that also allows you to specify the configuration for fetching tokens by setting the `tokenOptions` property.
|
|
271
|
-
|
|
272
|
-
If an HTTP call is made using `HttpClient` and there is no match in this configuration for that URL, then the interceptor will simply be bypassed and the call will be executed without a token attached in the `Authorization` header.
|
|
273
|
-
|
|
274
|
-
**Note:** We do this to help prevent tokens being unintentionally attached to requests to the wrong recipient, which is a serious security issue. Those recipients could then use that token to call the API as if it were your application.
|
|
275
|
-
|
|
276
|
-
In the event that requests should be made available for both anonymous and authenticated users, the `allowAnonymous` property can be set to `true`. When omitted, or set to `false`, requests that match the configuration, will not be executed when there is no access token available.
|
|
277
|
-
|
|
278
|
-
Here are some examples:
|
|
279
|
-
|
|
280
|
-
```js
|
|
281
|
-
import { HttpMethod } from '@auth0/auth0-angular';
|
|
282
|
-
|
|
283
|
-
// Modify your existing SDK configuration to include the httpInterceptor config
|
|
284
|
-
AuthModule.forRoot({
|
|
285
|
-
...
|
|
286
|
-
// The AuthHttpInterceptor configuration
|
|
287
|
-
httpInterceptor: {
|
|
288
|
-
allowedList: [
|
|
289
|
-
// Attach access tokens to any calls to '/api' (exact match)
|
|
290
|
-
'/api',
|
|
291
|
-
|
|
292
|
-
// Attach access tokens to any calls that start with '/api/'
|
|
293
|
-
'/api/*',
|
|
294
|
-
|
|
295
|
-
// Match anything starting with /api/products, but also allow for anonymous users.
|
|
296
|
-
{
|
|
297
|
-
uri: '/api/products/*',
|
|
298
|
-
allowAnonymous: true,
|
|
299
|
-
},
|
|
300
|
-
|
|
301
|
-
// Match anything starting with /api/accounts, but also specify the audience and scope the attached
|
|
302
|
-
// access token must have
|
|
303
|
-
{
|
|
304
|
-
uri: '/api/accounts/*',
|
|
305
|
-
tokenOptions: {
|
|
306
|
-
audience: 'http://my-api/',
|
|
307
|
-
scope: 'read:accounts',
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
|
|
311
|
-
// Matching on HTTP method
|
|
312
|
-
{
|
|
313
|
-
uri: '/api/orders',
|
|
314
|
-
httpMethod: HttpMethod.Post,
|
|
315
|
-
tokenOptions: {
|
|
316
|
-
audience: 'http://my-api/',
|
|
317
|
-
scope: 'write:orders',
|
|
318
|
-
},
|
|
319
|
-
},
|
|
320
|
-
|
|
321
|
-
// Using an absolute URI
|
|
322
|
-
{
|
|
323
|
-
uri: 'https://your-domain.auth0.com/api/v2/users',
|
|
324
|
-
tokenOptions: {
|
|
325
|
-
audience: 'https://your-domain.com/api/v2/',
|
|
326
|
-
scope: 'read:users',
|
|
327
|
-
},
|
|
328
|
-
},
|
|
329
|
-
],
|
|
330
|
-
},
|
|
331
|
-
});
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
> Under the hood, `tokenOptions` is passed as-is to [the `getTokenSilently` method](https://auth0.github.io/auth0-spa-js/classes/auth0client.html#gettokensilently) on the underlying SDK, so all the same options apply here.
|
|
335
|
-
|
|
336
|
-
**Uri Matching**
|
|
337
|
-
|
|
338
|
-
If you need more fine-grained control over the URI matching, you can provide a callback function to the `uriMatcher` property that takes a single `uri` argument (being [`HttpRequest.url`](https://angular.io/api/common/http/HttpRequest#url)) and returns a boolean. If this function returns true, then an access token is attached to the request in the ["Authorization" header](https://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-20#section-2.1). If it returns false, the request proceeds without the access token attached.
|
|
339
|
-
|
|
340
|
-
```
|
|
341
|
-
AuthModule.forRoot({
|
|
342
|
-
...
|
|
343
|
-
httpInterceptor: {
|
|
344
|
-
allowedList: [
|
|
345
|
-
{
|
|
346
|
-
uriMatcher: (uri) => uri.indexOf('/api/orders') > -1,
|
|
347
|
-
httpMethod: HttpMethod.Post,
|
|
348
|
-
tokenOptions: {
|
|
349
|
-
audience: 'http://my-api/',
|
|
350
|
-
scope: 'write:orders',
|
|
351
|
-
},
|
|
352
|
-
},
|
|
353
|
-
],
|
|
354
|
-
},
|
|
355
|
-
});
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
You might want to do this in scenarios where you need the token on multiple endpoints, but want to exclude it from only a few other endpoints. Instead of explicitly listing all endpoints that do need a token, a uriMatcher can be used to include all but the few endpoints that do not need a token attached to its requests.
|
|
359
|
-
|
|
360
|
-
#### Use HttpClient to make an API call
|
|
361
|
-
|
|
362
|
-
Finally, make your API call using the `HttpClient`. Access tokens are then attached automatically in the `Authorization` header:
|
|
363
|
-
|
|
364
|
-
```js
|
|
365
|
-
export class MyComponent {
|
|
366
|
-
constructor(private http: HttpClient) {}
|
|
367
|
-
|
|
368
|
-
callApi(): void {
|
|
369
|
-
this.http.get('/api').subscribe(result => console.log(result));
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
#### Handling errors
|
|
375
|
-
|
|
376
|
-
Whenever the SDK fails to retrieve an Access Token, either as part of the above interceptor or when manually calling `AuthService.getAccessTokenSilently` and `AuthService.getAccessTokenWithPopup`, it will emit the corresponding error in the `AuthService.error$` observable.
|
|
377
|
-
|
|
378
|
-
If you want to interact to these errors, subscribe to the `error$` observable and act accordingly.
|
|
379
|
-
|
|
380
|
-
```
|
|
381
|
-
ngOnInit() {
|
|
382
|
-
this.authService.error$.subscribe(error => {
|
|
383
|
-
// Handle Error here
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
A common reason you might want to handle the above errors, emitted by the `error$` observable, is to re-login the user when the SDK throws a `login_required` error.
|
|
389
|
-
|
|
390
|
-
```
|
|
391
|
-
ngOnInit() {
|
|
392
|
-
this.authService.error$.pipe(
|
|
393
|
-
filter((e) => e instanceof GenericError && e.error === 'login_required'),
|
|
394
|
-
mergeMap(() => this.authService.loginWithRedirect())
|
|
395
|
-
).subscribe();
|
|
396
|
-
}
|
|
397
|
-
```
|
|
398
|
-
|
|
399
|
-
### Dynamic Configuration
|
|
86
|
+
#### Dynamic configuration
|
|
400
87
|
|
|
401
88
|
Instead of using `AuthModule.forRoot` to specify auth configuration, you can provide a factory function using `APP_INITIALIZER` to load your config from an external source before the auth module is loaded, and provide your configuration using `AuthClientConfig.set`.
|
|
402
89
|
|
|
@@ -405,8 +92,6 @@ The configuration will only be used initially when the SDK is instantiated. Any
|
|
|
405
92
|
> :information_source: Any request made through an instance of `HttpClient` that got instantiated by Angular, will use all of the configured interceptors, including our `AuthHttpInterceptor`. Because the `AuthHttpInterceptor` requires the existence of configuration settings, the request for retrieving those dynamic configuration settings should ensure it's not using any of those interceptors. In Angular, this can be done by manually instantiating `HttpClient` using an injected `HttpBackend` instance.
|
|
406
93
|
|
|
407
94
|
```js
|
|
408
|
-
// app.module.ts
|
|
409
|
-
// ---------------------------
|
|
410
95
|
import { AuthModule, AuthClientConfig } from '@auth0/auth0-angular';
|
|
411
96
|
|
|
412
97
|
// Provide an initializer function that returns a Promise
|
|
@@ -418,88 +103,33 @@ function configInitializer(
|
|
|
418
103
|
new HttpClient(handler)
|
|
419
104
|
.get('/config')
|
|
420
105
|
.toPromise()
|
|
421
|
-
|
|
106
|
+
// Set the config that was loaded asynchronously here
|
|
107
|
+
.then((loadedConfig: any) => config.set(loadedConfig));
|
|
422
108
|
}
|
|
423
109
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
### Using multiple OAuth providers
|
|
442
|
-
|
|
443
|
-
If your application uses multiple OAuth providers, you may need to use multiple callback paths as well, one for each OAuth provider.
|
|
444
|
-
To ensure the SDK does not process the callback for any provider other than Auth0, configure the AuthModule by setting the `skipRedirectCallback` property as follows:
|
|
445
|
-
|
|
446
|
-
```js
|
|
447
|
-
AuthModule.forRoot({
|
|
448
|
-
skipRedirectCallback: window.location.pathname === '/other-callback',
|
|
449
|
-
});
|
|
450
|
-
```
|
|
451
|
-
|
|
452
|
-
**Note**: In the above example, `/other-callback` is an existing route that will be called by any other OAuth provider with a `code` (or `error` in case something went wrong) and `state`.
|
|
453
|
-
|
|
454
|
-
### Organizations
|
|
455
|
-
|
|
456
|
-
[Organizations](https://auth0.com/docs/organizations) is a set of features that provide better support for developers who build and maintain SaaS and Business-to-Business (B2B) applications.
|
|
457
|
-
|
|
458
|
-
Using Organizations, you can:
|
|
459
|
-
|
|
460
|
-
- Represent teams, business customers, partner companies, or any logical grouping of users that should have different ways of accessing your applications, as organizations.
|
|
461
|
-
|
|
462
|
-
- Manage their membership in a variety of ways, including user invitation.
|
|
463
|
-
|
|
464
|
-
- Configure branded, federated login flows for each organization.
|
|
465
|
-
|
|
466
|
-
- Implement role-based access control, such that users can have different roles when authenticating in the context of different organizations.
|
|
467
|
-
|
|
468
|
-
- Build administration capabilities into your products, using Organizations APIs, so that those businesses can manage their own organizations.
|
|
469
|
-
|
|
470
|
-
Note that Organizations is currently only available to customers on our Enterprise and Startup subscription plans.
|
|
471
|
-
|
|
472
|
-
#### Log in to an organization
|
|
473
|
-
|
|
474
|
-
Log in to an organization by specifying the `organization` parameter importing the `AuthModule`:
|
|
475
|
-
|
|
476
|
-
```
|
|
477
|
-
AuthModule.forRoot({
|
|
478
|
-
domain: 'YOUR_AUTH0_DOMAIN',
|
|
479
|
-
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
480
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
481
|
-
}),
|
|
482
|
-
```
|
|
483
|
-
|
|
484
|
-
You can also specify the organization when logging in:
|
|
485
|
-
|
|
486
|
-
```
|
|
487
|
-
// Using a redirect
|
|
488
|
-
this.auth.loginWithRedirect({
|
|
489
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
490
|
-
});
|
|
491
|
-
|
|
492
|
-
// Using a popup window
|
|
493
|
-
this.auth.loginWithPopup({
|
|
494
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
495
|
-
});
|
|
110
|
+
export class AppModule {
|
|
111
|
+
// ...
|
|
112
|
+
imports: [
|
|
113
|
+
HttpClientModule,
|
|
114
|
+
AuthModule.forRoot(), // <- don't pass any config here
|
|
115
|
+
],
|
|
116
|
+
providers: [
|
|
117
|
+
{
|
|
118
|
+
provide: APP_INITIALIZER,
|
|
119
|
+
useFactory: configInitializer, // <- pass your initializer function here
|
|
120
|
+
deps: [HttpBackend, AuthClientConfig],
|
|
121
|
+
multi: true,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
// ...
|
|
125
|
+
}
|
|
496
126
|
```
|
|
497
127
|
|
|
498
|
-
|
|
128
|
+
### Add login to your application
|
|
499
129
|
|
|
500
|
-
|
|
130
|
+
To log the user into the application, inject the `AuthService` and call its `loginWithRedirect` method.
|
|
501
131
|
|
|
502
|
-
```
|
|
132
|
+
```ts
|
|
503
133
|
import { Component } from '@angular/core';
|
|
504
134
|
import { AuthService } from '@auth0/auth0-angular';
|
|
505
135
|
|
|
@@ -509,88 +139,50 @@ import { AuthService } from '@auth0/auth0-angular';
|
|
|
509
139
|
styleUrls: ['./app.component.css'],
|
|
510
140
|
})
|
|
511
141
|
export class AppComponent {
|
|
512
|
-
constructor(public auth: AuthService
|
|
513
|
-
|
|
514
|
-
loginWithRedirect(): void {
|
|
515
|
-
const { organization, invitation } = this.activatedRoute.snapshot.params;
|
|
142
|
+
constructor(public auth: AuthService) {}
|
|
516
143
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
invitation
|
|
520
|
-
});
|
|
144
|
+
loginWithRedirect() {
|
|
145
|
+
this.auth.loginWithRedirect();
|
|
521
146
|
}
|
|
522
|
-
}
|
|
523
147
|
```
|
|
524
148
|
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
This library makes use of the `window` object in a couple of places during initialization, as well as `sessionStorage` in the underlying Auth0 SPA SDK, and thus [will have problems](https://github.com/angular/universal/blob/master/docs/gotchas.md#window-is-not-defined) when being used in an Angular Universal project. The recommendation currently is to only import this library into a module that is to be used in the browser, and omit it from any module that is to participate in a server-side environment.
|
|
528
|
-
|
|
529
|
-
See [Guards, and creating separate modules](https://github.com/angular/universal/blob/master/docs/gotchas.md#strategy-2-guards) in the Angular Universal "Gotchas" document.
|
|
530
|
-
|
|
531
|
-
## Development
|
|
149
|
+
By default the application will ask Auth0 to redirect back to the root URL of your application after authentication. This can be configured by setting the [redirectUri](https://auth0.github.io/auth0-angular/interfaces/auth_config.authconfig.html#redirecturi) option.
|
|
532
150
|
|
|
533
|
-
|
|
151
|
+
For more code samples on how to integrate the **auth0-angular** SDK in your **Angular** application, have a look at our [examples](https://github.com/auth0/auth0-angular/tree/master/EXAMPLES.md).
|
|
534
152
|
|
|
535
|
-
|
|
153
|
+
## API reference
|
|
536
154
|
|
|
537
|
-
|
|
155
|
+
Explore public API's available in auth0-angular.
|
|
538
156
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
### Running end-to-end tests
|
|
542
|
-
|
|
543
|
-
The end-to-end tests are executed using [Cypress](https://www.cypress.io/) against the built-in playground app.
|
|
544
|
-
|
|
545
|
-
The E2E tests require that the user password be specified as an environment variable. This is already set up in the CI environment, but locally you can do:
|
|
546
|
-
|
|
547
|
-
```
|
|
548
|
-
CYPRESS_INTEGRATION_PASSWORD=<password> ng e2e
|
|
549
|
-
```
|
|
157
|
+
- [AuthService](https://auth0.github.io/auth0-angular/classes/auth_service.authservice.html) - service used to interact with the SDK.
|
|
158
|
+
- [AuthConfig](https://auth0.github.io/auth0-angular/interfaces/auth_config.authconfig.html) - used to configure the SDK.
|
|
550
159
|
|
|
551
|
-
|
|
160
|
+
## Feedback
|
|
552
161
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
#### Running an express server
|
|
556
|
-
|
|
557
|
-
An express server can be started by running `npm run server:api`, which can be used to make testing Http Interceptors easier.
|
|
558
|
-
The express server exposes a single endpoint at `http://localhost:3001/api/external` that needs to be called with an `Authorization` header containing a token for the corresponding `domain` and `audience`, configurable in [`api-server.js`](api-server.js).
|
|
559
|
-
|
|
560
|
-
The playground application is preconfigured to call the above endpoint when clicking the `Call external API` button.
|
|
561
|
-
|
|
562
|
-
## Further help
|
|
563
|
-
|
|
564
|
-
To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md).
|
|
565
|
-
|
|
566
|
-
## Contributing
|
|
162
|
+
### Contributing
|
|
567
163
|
|
|
568
164
|
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
|
|
569
165
|
|
|
570
166
|
- [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)
|
|
571
167
|
- [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
|
|
572
168
|
|
|
573
|
-
|
|
169
|
+
### Raise an issue
|
|
574
170
|
|
|
575
|
-
|
|
171
|
+
To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-angular/issues).
|
|
576
172
|
|
|
577
|
-
|
|
173
|
+
### Vulnerability Reporting
|
|
578
174
|
|
|
579
175
|
Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/responsible-disclosure-policy) details the procedure for disclosing security issues.
|
|
580
176
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
## License
|
|
595
|
-
|
|
596
|
-
This project is licensed under the MIT license. See the [LICENSE](https://github.com/auth0/auth0-angular/blob/master/LICENSE) file for more info.
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
<p align="center">
|
|
180
|
+
<picture>
|
|
181
|
+
<source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
182
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
|
|
183
|
+
<img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
184
|
+
</picture>
|
|
185
|
+
</p>
|
|
186
|
+
<p align="center">Auth0 is an easy to implement, adaptable authentication and authorization platform. To learn more checkout <a href="https://auth0.com/why-auth0">Why Auth0?</a></p>
|
|
187
|
+
<p align="center">
|
|
188
|
+
This project is licensed under the MIT license. See the <a href="https://github.com/auth0/auth0-angular/tree/master/LICENSE"> LICENSE</a> file for more info.</p>
|