@auth0/auth0-angular 1.11.0 → 2.0.0-beta.0
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 +89 -499
- package/{esm2015/auth0-auth0-angular.js → esm2020/auth0-auth0-angular.mjs} +0 -0
- package/{esm2015/lib/abstract-navigator.js → esm2020/lib/abstract-navigator.mjs} +6 -5
- package/esm2020/lib/auth.client.mjs +23 -0
- package/esm2020/lib/auth.config.mjs +97 -0
- package/{esm2015/lib/auth.guard.js → esm2020/lib/auth.guard.mjs} +3 -3
- package/esm2020/lib/auth.interceptor.mjs +134 -0
- package/esm2020/lib/auth.module.mjs +39 -0
- package/esm2020/lib/auth.service.mjs +229 -0
- package/esm2020/lib/auth.state.mjs +100 -0
- package/esm2020/lib/interfaces.mjs +2 -0
- package/esm2020/public-api.mjs +13 -0
- package/{esm2015/useragent.js → esm2020/useragent.mjs} +2 -2
- package/fesm2015/auth0-auth0-angular.mjs +678 -0
- package/fesm2015/auth0-auth0-angular.mjs.map +1 -0
- package/{fesm2015/auth0-auth0-angular.js → fesm2020/auth0-auth0-angular.mjs} +80 -135
- package/fesm2020/auth0-auth0-angular.mjs.map +1 -0
- package/lib/abstract-navigator.d.ts +1 -0
- package/lib/auth.config.d.ts +4 -134
- package/lib/auth.interceptor.d.ts +7 -1
- package/lib/auth.module.d.ts +1 -0
- package/lib/auth.service.d.ts +8 -67
- package/lib/auth.state.d.ts +3 -0
- package/lib/interfaces.d.ts +5 -0
- package/package.json +23 -10
- package/public-api.d.ts +1 -0
- package/bundles/auth0-auth0-angular.umd.js +0 -1148
- package/bundles/auth0-auth0-angular.umd.js.map +0 -1
- package/esm2015/lib/auth.client.js +0 -22
- package/esm2015/lib/auth.config.js +0 -95
- package/esm2015/lib/auth.interceptor.js +0 -129
- package/esm2015/lib/auth.module.js +0 -38
- package/esm2015/lib/auth.service.js +0 -294
- package/esm2015/lib/auth.state.js +0 -99
- package/esm2015/public-api.js +0 -12
- package/fesm2015/auth0-auth0-angular.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,406 +1,91 @@
|
|
|
1
|
-

|
|
3
|
-
[](https://circleci.com/gh/auth0/auth0-angular)
|
|
4
|
-
[](https://codecov.io/gh/auth0/auth0-angular)
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
# Auth0 Angular SDK
|
|
1
|
+

|
|
8
2
|
|
|
9
3
|
A library for integrating [Auth0](https://auth0.com) into an Angular application.
|
|
10
4
|
|
|
11
|
-
|
|
5
|
+
> :warning: Please be aware that v2 is currently in [**Beta**](https://auth0.com/docs/troubleshoot/product-lifecycle/product-release-stages). Whilst we encourage you to test the update within your applications, we do no recommend using this version in production yet. Please follow the [migration guide](./MIGRATION_GUIDE.md) when updating your application.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
[](https://codecov.io/gh/auth0/auth0-angular)
|
|
9
|
+

|
|
10
|
+
[](https://opensource.org/licenses/MIT)
|
|
11
|
+
[](https://circleci.com/gh/auth0/auth0-angular)
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
- [Supported Angular versions](#supported-angular-versions)
|
|
15
|
-
- [Installation](#installation)
|
|
16
|
-
- [Getting Started](#getting-started)
|
|
17
|
-
- [Angular Universal](#angular-universal)
|
|
18
|
-
- [Development](#development)
|
|
19
|
-
- [Contributing](#contributing)
|
|
20
|
-
- [Support + Feedback](#support--feedback)
|
|
21
|
-
- [Vulnerability Reporting](#vulnerability-reporting)
|
|
22
|
-
- [What is Auth0](#what-is-auth0)
|
|
23
|
-
- [License](#license)
|
|
13
|
+
📚 [Documentation](#documentation) - 🚀 [Getting Started](#getting-started) - 💻 [API Reference](#api-reference) - 💬 [Feedback](#feedback)
|
|
24
14
|
|
|
25
15
|
## Documentation
|
|
26
16
|
|
|
27
|
-
- [
|
|
28
|
-
- [
|
|
17
|
+
- [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.
|
|
18
|
+
- [Sample App](https://github.com/auth0-samples/auth0-angular-samples/tree/master/Sample-01) - a full-fledged Angular application integrated with Auth0.
|
|
19
|
+
- [FAQs](https://github.com/auth0/auth0-angular/tree/master/FAQ.md) - frequently asked questions about the auth0-angular SDK.
|
|
20
|
+
- [Examples](https://github.com/auth0/auth0-angular/tree/master/EXAMPLES.md) - code samples for common Angular authentication scenario's.
|
|
21
|
+
- [Docs site](https://www.auth0.com/docs) - explore our docs site and learn more about Auth0.
|
|
22
|
+
|
|
23
|
+
## Getting started
|
|
24
|
+
|
|
25
|
+
### Requirements
|
|
29
26
|
|
|
30
|
-
## Supported Angular versions
|
|
31
27
|
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.
|
|
32
28
|
|
|
33
|
-
|
|
29
|
+
### Installation
|
|
34
30
|
|
|
35
31
|
Using npm:
|
|
36
32
|
|
|
37
33
|
```sh
|
|
38
|
-
npm install @auth0/auth0-angular
|
|
34
|
+
npm install @auth0/auth0-angular@beta
|
|
39
35
|
```
|
|
40
36
|
|
|
41
37
|
We also have `ng-add` support, so the library can also be installed using the Angular CLI:
|
|
42
38
|
|
|
43
39
|
```sh
|
|
44
|
-
ng add @auth0/auth0-angular
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Getting Started
|
|
48
|
-
|
|
49
|
-
- [Register the authentication module](#register-the-authentication-module)
|
|
50
|
-
- [Add login to your application](#add-login-to-your-application)
|
|
51
|
-
- [Add logout to your application](#add-logout-to-your-application)
|
|
52
|
-
- [Display the user profile](#display-the-user-profile)
|
|
53
|
-
- [Protect a route](#protect-a-route)
|
|
54
|
-
- [Call an API](#call-an-api)
|
|
55
|
-
- [Dynamic configuration](#dynamic-configuration)
|
|
56
|
-
- [Using multiple OAuth providers](#using-multiple-oauth-providers)
|
|
57
|
-
|
|
58
|
-
### Register the authentication module
|
|
59
|
-
|
|
60
|
-
Install the SDK into your application by importing `AuthModule` and configuring with your Auth0 domain and client ID:
|
|
61
|
-
|
|
62
|
-
```js
|
|
63
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
64
|
-
import { NgModule } from '@angular/core';
|
|
65
|
-
import { AppComponent } from './app.component';
|
|
66
|
-
|
|
67
|
-
// Import the module from the SDK
|
|
68
|
-
import { AuthModule } from '@auth0/auth0-angular';
|
|
69
|
-
|
|
70
|
-
@NgModule({
|
|
71
|
-
declarations: [AppComponent],
|
|
72
|
-
imports: [
|
|
73
|
-
BrowserModule,
|
|
74
|
-
|
|
75
|
-
// Import the module into the application, with configuration
|
|
76
|
-
AuthModule.forRoot({
|
|
77
|
-
domain: 'YOUR_AUTH0_DOMAIN',
|
|
78
|
-
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
79
|
-
}),
|
|
80
|
-
],
|
|
81
|
-
|
|
82
|
-
bootstrap: [AppComponent],
|
|
83
|
-
})
|
|
84
|
-
export class AppModule {}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### Add login to your application
|
|
88
|
-
|
|
89
|
-
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.
|
|
90
|
-
|
|
91
|
-
```js
|
|
92
|
-
import { Component } from '@angular/core';
|
|
93
|
-
|
|
94
|
-
// Import the AuthService type from the SDK
|
|
95
|
-
import { AuthService } from '@auth0/auth0-angular';
|
|
96
|
-
|
|
97
|
-
@Component({
|
|
98
|
-
selector: 'app-root',
|
|
99
|
-
templateUrl: './app.component.html',
|
|
100
|
-
styleUrls: ['./app.component.css'],
|
|
101
|
-
})
|
|
102
|
-
export class AppComponent {
|
|
103
|
-
title = 'My App';
|
|
104
|
-
|
|
105
|
-
// Inject the authentication service into your component through the constructor
|
|
106
|
-
constructor(public auth: AuthService) {}
|
|
107
|
-
|
|
108
|
-
loginWithRedirect(): void {
|
|
109
|
-
// Call this to redirect the user to the login page
|
|
110
|
-
this.auth.loginWithRedirect();
|
|
111
|
-
}
|
|
112
|
-
}
|
|
40
|
+
ng add @auth0/auth0-angular@beta
|
|
113
41
|
```
|
|
114
42
|
|
|
115
|
-
|
|
43
|
+
### Configure Auth0
|
|
116
44
|
|
|
117
|
-
|
|
45
|
+
Create a **Single Page Application** in the [Auth0 Dashboard](https://manage.auth0.com/#/applications).
|
|
118
46
|
|
|
119
|
-
|
|
120
|
-
<button
|
|
121
|
-
*ngIf="(auth.isAuthenticated$ | async) === false"
|
|
122
|
-
(click)="loginWithRedirect()"
|
|
47
|
+
> **If you're using an existing application**, verify that you have configured the following settings in your Single Page Application:
|
|
123
48
|
>
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
Add a `logout` method to your component and call the SDK's `logout` method:
|
|
131
|
-
|
|
132
|
-
```js
|
|
133
|
-
logout(): void {
|
|
134
|
-
// Call this to log the user out of the application
|
|
135
|
-
this.auth.logout({ returnTo: window.location.origin });
|
|
136
|
-
}
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
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:
|
|
140
|
-
|
|
141
|
-
```html
|
|
142
|
-
<button *ngIf="auth.isAuthenticated$ | async" (click)="logout()">
|
|
143
|
-
Log out
|
|
144
|
-
</button>
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Display the user profile
|
|
148
|
-
|
|
149
|
-
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:
|
|
150
|
-
|
|
151
|
-
```html
|
|
152
|
-
<ul *ngIf="auth.user$ | async as user">
|
|
153
|
-
<li>{{ user.name }}</li>
|
|
154
|
-
<li>{{ user.email }}</li>
|
|
155
|
-
</ul>
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Access ID token claims
|
|
159
|
-
|
|
160
|
-
Access the `idTokenClaims$` observable on the `AuthService` instance to retrieve the ID token claims. Like the `user$` observable, this observable already heeds the `isAuthenticated$` observable, so you do not need to check if the user is authenticated before using it:
|
|
161
|
-
|
|
162
|
-
```js
|
|
163
|
-
authService.idTokenClaims$.subscribe((claims) => console.log(claims));
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
### Handle errors
|
|
167
|
-
|
|
168
|
-
Errors in the login flow can be captured by subscribing to the `error$` observable:
|
|
169
|
-
|
|
170
|
-
```js
|
|
171
|
-
authService.error$.subscribe((error) => console.log(error));
|
|
172
|
-
```
|
|
49
|
+
> - Click on the "Settings" tab of your application's page.
|
|
50
|
+
> - Ensure that "Token Endpoint Authentication Method" under "Application Properties" is set to "None"
|
|
51
|
+
> - Scroll down and click on the "Show Advanced Settings" link.
|
|
52
|
+
> - Under "Advanced Settings", click on the "OAuth" tab.
|
|
53
|
+
> - Ensure that "JsonWebToken Signature Algorithm" is set to `RS256` and that "OIDC Conformant" is enabled.
|
|
173
54
|
|
|
174
|
-
|
|
55
|
+
Next, configure the following URLs for your application under the "Application URIs" section of the "Settings" page:
|
|
175
56
|
|
|
176
|
-
|
|
57
|
+
- **Allowed Callback URLs**: `http://localhost:4200`
|
|
58
|
+
- **Allowed Logout URLs**: `http://localhost:4200`
|
|
59
|
+
- **Allowed Web Origins**: `http://localhost:4200`
|
|
177
60
|
|
|
178
|
-
|
|
61
|
+
> 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.
|
|
179
62
|
|
|
180
|
-
|
|
181
|
-
import { NgModule } from '@angular/core';
|
|
182
|
-
import { Routes, RouterModule } from '@angular/router';
|
|
183
|
-
import { HomeComponent } from './unprotected/unprotected.component';
|
|
184
|
-
import { ProtectedComponent } from './protected/protected.component';
|
|
185
|
-
|
|
186
|
-
// Import the authentication guard
|
|
187
|
-
import { AuthGuard } from '@auth0/auth0-angular';
|
|
188
|
-
|
|
189
|
-
const routes: Routes = [
|
|
190
|
-
{
|
|
191
|
-
path: 'protected',
|
|
192
|
-
component: ProtectedComponent,
|
|
193
|
-
|
|
194
|
-
// Protect a route by registering the auth guard in the `canActivate` hook
|
|
195
|
-
canActivate: [AuthGuard],
|
|
196
|
-
},
|
|
197
|
-
{
|
|
198
|
-
path: '',
|
|
199
|
-
component: HomeComponent,
|
|
200
|
-
pathMatch: 'full',
|
|
201
|
-
},
|
|
202
|
-
];
|
|
203
|
-
|
|
204
|
-
@NgModule({
|
|
205
|
-
imports: [RouterModule.forRoot(routes)],
|
|
206
|
-
exports: [RouterModule],
|
|
207
|
-
})
|
|
208
|
-
export class AppRoutingModule {}
|
|
209
|
-
```
|
|
63
|
+
Take note of the **Client ID** and **Domain** values under the "Basic Information" section. You'll need these values in the next step.
|
|
210
64
|
|
|
211
|
-
###
|
|
65
|
+
### Configure the SDK
|
|
212
66
|
|
|
213
|
-
|
|
67
|
+
#### Static configuration
|
|
214
68
|
|
|
215
|
-
|
|
216
|
-
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.
|
|
217
|
-
Set the `audience`, when calling `AuthModule.forRoot()`, to the **API Identifier** of the API from within your Auth0 dashboard.
|
|
69
|
+
Install the SDK into your application by importing `AuthModule.forRoot()` and configuring with your Auth0 domain and client id:
|
|
218
70
|
|
|
219
|
-
```
|
|
220
|
-
import { BrowserModule } from '@angular/platform-browser';
|
|
71
|
+
```ts
|
|
221
72
|
import { NgModule } from '@angular/core';
|
|
222
|
-
import { AppComponent } from './app.component';
|
|
223
|
-
|
|
224
|
-
// Import the module from the SDK
|
|
225
73
|
import { AuthModule } from '@auth0/auth0-angular';
|
|
226
74
|
|
|
227
75
|
@NgModule({
|
|
228
|
-
|
|
76
|
+
// ...
|
|
229
77
|
imports: [
|
|
230
|
-
BrowserModule,
|
|
231
|
-
|
|
232
|
-
// Import the module into the application, with configuration
|
|
233
78
|
AuthModule.forRoot({
|
|
234
79
|
domain: 'YOUR_AUTH0_DOMAIN',
|
|
235
80
|
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
236
|
-
audience: 'YOUR_AUTH0_API_IDENTIFIER',
|
|
237
81
|
}),
|
|
238
82
|
],
|
|
239
|
-
|
|
240
|
-
bootstrap: [AppComponent],
|
|
83
|
+
// ...
|
|
241
84
|
})
|
|
242
85
|
export class AppModule {}
|
|
243
86
|
```
|
|
244
87
|
|
|
245
|
-
####
|
|
246
|
-
|
|
247
|
-
First, register the interceptor with your application module, along with the `HttpClientModule`.
|
|
248
|
-
|
|
249
|
-
**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.
|
|
250
|
-
|
|
251
|
-
```js
|
|
252
|
-
// Import the interceptor module and the Angular types you'll need
|
|
253
|
-
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
|
|
254
|
-
import { AuthHttpInterceptor } from '@auth0/auth0-angular';
|
|
255
|
-
|
|
256
|
-
// Register the interceptor with your app module in the `providers` array
|
|
257
|
-
@NgModule({
|
|
258
|
-
declarations: [],
|
|
259
|
-
imports: [
|
|
260
|
-
BrowserModule,
|
|
261
|
-
HttpClientModule, // Register this so that you can make API calls using HttpClient
|
|
262
|
-
AppRoutingModule,
|
|
263
|
-
AuthModule.forRoot(...),
|
|
264
|
-
],
|
|
265
|
-
providers: [
|
|
266
|
-
{ provide: HTTP_INTERCEPTORS, useClass: AuthHttpInterceptor, multi: true },
|
|
267
|
-
],
|
|
268
|
-
bootstrap: [AppComponent],
|
|
269
|
-
})
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
#### Configure AuthHttpInterceptor to attach access tokens
|
|
273
|
-
|
|
274
|
-
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.
|
|
275
|
-
|
|
276
|
-
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.
|
|
277
|
-
|
|
278
|
-
**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.
|
|
279
|
-
|
|
280
|
-
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.
|
|
281
|
-
|
|
282
|
-
Here are some examples:
|
|
283
|
-
|
|
284
|
-
```js
|
|
285
|
-
import { HttpMethod } from '@auth0/auth0-angular';
|
|
286
|
-
|
|
287
|
-
// Modify your existing SDK configuration to include the httpInterceptor config
|
|
288
|
-
AuthModule.forRoot({
|
|
289
|
-
...
|
|
290
|
-
// The AuthHttpInterceptor configuration
|
|
291
|
-
httpInterceptor: {
|
|
292
|
-
allowedList: [
|
|
293
|
-
// Attach access tokens to any calls to '/api' (exact match)
|
|
294
|
-
'/api',
|
|
295
|
-
|
|
296
|
-
// Attach access tokens to any calls that start with '/api/'
|
|
297
|
-
'/api/*',
|
|
298
|
-
|
|
299
|
-
// Match anything starting with /api/products, but also allow for anonymous users.
|
|
300
|
-
{
|
|
301
|
-
uri: '/api/products/*',
|
|
302
|
-
allowAnonymous: true,
|
|
303
|
-
},
|
|
304
|
-
|
|
305
|
-
// Match anything starting with /api/accounts, but also specify the audience and scope the attached
|
|
306
|
-
// access token must have
|
|
307
|
-
{
|
|
308
|
-
uri: '/api/accounts/*',
|
|
309
|
-
tokenOptions: {
|
|
310
|
-
audience: 'http://my-api/',
|
|
311
|
-
scope: 'read:accounts',
|
|
312
|
-
},
|
|
313
|
-
},
|
|
314
|
-
|
|
315
|
-
// Matching on HTTP method
|
|
316
|
-
{
|
|
317
|
-
uri: '/api/orders',
|
|
318
|
-
httpMethod: HttpMethod.Post,
|
|
319
|
-
tokenOptions: {
|
|
320
|
-
audience: 'http://my-api/',
|
|
321
|
-
scope: 'write:orders',
|
|
322
|
-
},
|
|
323
|
-
},
|
|
324
|
-
|
|
325
|
-
// Using an absolute URI
|
|
326
|
-
{
|
|
327
|
-
uri: 'https://your-domain.auth0.com/api/v2/users',
|
|
328
|
-
tokenOptions: {
|
|
329
|
-
audience: 'https://your-domain.com/api/v2/',
|
|
330
|
-
scope: 'read:users',
|
|
331
|
-
},
|
|
332
|
-
},
|
|
333
|
-
],
|
|
334
|
-
},
|
|
335
|
-
});
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
> 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.
|
|
339
|
-
|
|
340
|
-
**Uri Matching**
|
|
341
|
-
|
|
342
|
-
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.
|
|
343
|
-
|
|
344
|
-
```
|
|
345
|
-
AuthModule.forRoot({
|
|
346
|
-
...
|
|
347
|
-
httpInterceptor: {
|
|
348
|
-
allowedList: [
|
|
349
|
-
{
|
|
350
|
-
uriMatcher: (uri) => uri.indexOf('/api/orders') > -1,
|
|
351
|
-
httpMethod: HttpMethod.Post,
|
|
352
|
-
tokenOptions: {
|
|
353
|
-
audience: 'http://my-api/',
|
|
354
|
-
scope: 'write:orders',
|
|
355
|
-
},
|
|
356
|
-
},
|
|
357
|
-
],
|
|
358
|
-
},
|
|
359
|
-
});
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
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.
|
|
363
|
-
|
|
364
|
-
#### Use HttpClient to make an API call
|
|
365
|
-
|
|
366
|
-
Finally, make your API call using the `HttpClient`. Access tokens are then attached automatically in the `Authorization` header:
|
|
367
|
-
|
|
368
|
-
```js
|
|
369
|
-
export class MyComponent {
|
|
370
|
-
constructor(private http: HttpClient) {}
|
|
371
|
-
|
|
372
|
-
callApi(): void {
|
|
373
|
-
this.http.get('/api').subscribe(result => console.log(result));
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
#### Handling errors
|
|
379
|
-
|
|
380
|
-
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.
|
|
381
|
-
|
|
382
|
-
If you want to interact to these errors, subscribe to the `error$` observable and act accordingly.
|
|
383
|
-
|
|
384
|
-
```
|
|
385
|
-
ngOnInit() {
|
|
386
|
-
this.authService.error$.subscribe(error => {
|
|
387
|
-
// Handle Error here
|
|
388
|
-
});
|
|
389
|
-
}
|
|
390
|
-
```
|
|
391
|
-
|
|
392
|
-
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.
|
|
393
|
-
|
|
394
|
-
```
|
|
395
|
-
ngOnInit() {
|
|
396
|
-
this.authService.error$.pipe(
|
|
397
|
-
filter((e) => e instanceof GenericError && e.error === 'login_required'),
|
|
398
|
-
mergeMap(() => this.authService.loginWithRedirect())
|
|
399
|
-
).subscribe();
|
|
400
|
-
}
|
|
401
|
-
```
|
|
402
|
-
|
|
403
|
-
### Dynamic Configuration
|
|
88
|
+
#### Dynamic configuration
|
|
404
89
|
|
|
405
90
|
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`.
|
|
406
91
|
|
|
@@ -409,8 +94,6 @@ The configuration will only be used initially when the SDK is instantiated. Any
|
|
|
409
94
|
> :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.
|
|
410
95
|
|
|
411
96
|
```js
|
|
412
|
-
// app.module.ts
|
|
413
|
-
// ---------------------------
|
|
414
97
|
import { AuthModule, AuthClientConfig } from '@auth0/auth0-angular';
|
|
415
98
|
|
|
416
99
|
// Provide an initializer function that returns a Promise
|
|
@@ -422,88 +105,33 @@ function configInitializer(
|
|
|
422
105
|
new HttpClient(handler)
|
|
423
106
|
.get('/config')
|
|
424
107
|
.toPromise()
|
|
425
|
-
|
|
108
|
+
// Set the config that was loaded asynchronously here
|
|
109
|
+
.then((loadedConfig: any) => config.set(loadedConfig));
|
|
426
110
|
}
|
|
427
111
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
### Using multiple OAuth providers
|
|
446
|
-
|
|
447
|
-
If your application uses multiple OAuth providers, you may need to use multiple callback paths as well, one for each OAuth provider.
|
|
448
|
-
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:
|
|
449
|
-
|
|
450
|
-
```js
|
|
451
|
-
AuthModule.forRoot({
|
|
452
|
-
skipRedirectCallback: window.location.pathname === '/other-callback',
|
|
453
|
-
});
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
**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`.
|
|
457
|
-
|
|
458
|
-
### Organizations
|
|
459
|
-
|
|
460
|
-
[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.
|
|
461
|
-
|
|
462
|
-
Using Organizations, you can:
|
|
463
|
-
|
|
464
|
-
- Represent teams, business customers, partner companies, or any logical grouping of users that should have different ways of accessing your applications, as organizations.
|
|
465
|
-
|
|
466
|
-
- Manage their membership in a variety of ways, including user invitation.
|
|
467
|
-
|
|
468
|
-
- Configure branded, federated login flows for each organization.
|
|
469
|
-
|
|
470
|
-
- Implement role-based access control, such that users can have different roles when authenticating in the context of different organizations.
|
|
471
|
-
|
|
472
|
-
- Build administration capabilities into your products, using Organizations APIs, so that those businesses can manage their own organizations.
|
|
473
|
-
|
|
474
|
-
Note that Organizations is currently only available to customers on our Enterprise and Startup subscription plans.
|
|
475
|
-
|
|
476
|
-
#### Log in to an organization
|
|
477
|
-
|
|
478
|
-
Log in to an organization by specifying the `organization` parameter importing the `AuthModule`:
|
|
479
|
-
|
|
480
|
-
```
|
|
481
|
-
AuthModule.forRoot({
|
|
482
|
-
domain: 'YOUR_AUTH0_DOMAIN',
|
|
483
|
-
clientId: 'YOUR_AUTH0_CLIENT_ID',
|
|
484
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
485
|
-
}),
|
|
486
|
-
```
|
|
487
|
-
|
|
488
|
-
You can also specify the organization when logging in:
|
|
489
|
-
|
|
490
|
-
```
|
|
491
|
-
// Using a redirect
|
|
492
|
-
this.auth.loginWithRedirect({
|
|
493
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
494
|
-
});
|
|
495
|
-
|
|
496
|
-
// Using a popup window
|
|
497
|
-
this.auth.loginWithPopup({
|
|
498
|
-
organization: 'YOUR_ORGANIZATION_ID'
|
|
499
|
-
});
|
|
112
|
+
export class AppModule {
|
|
113
|
+
// ...
|
|
114
|
+
imports: [
|
|
115
|
+
HttpClientModule,
|
|
116
|
+
AuthModule.forRoot(), // <- don't pass any config here
|
|
117
|
+
],
|
|
118
|
+
providers: [
|
|
119
|
+
{
|
|
120
|
+
provide: APP_INITIALIZER,
|
|
121
|
+
useFactory: configInitializer, // <- pass your initializer function here
|
|
122
|
+
deps: [HttpBackend, AuthClientConfig],
|
|
123
|
+
multi: true,
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
// ...
|
|
127
|
+
}
|
|
500
128
|
```
|
|
501
129
|
|
|
502
|
-
|
|
130
|
+
### Add login to your application
|
|
503
131
|
|
|
504
|
-
|
|
132
|
+
To log the user into the application, inject the `AuthService` and call its `loginWithRedirect` method.
|
|
505
133
|
|
|
506
|
-
```
|
|
134
|
+
```ts
|
|
507
135
|
import { Component } from '@angular/core';
|
|
508
136
|
import { AuthService } from '@auth0/auth0-angular';
|
|
509
137
|
|
|
@@ -513,88 +141,50 @@ import { AuthService } from '@auth0/auth0-angular';
|
|
|
513
141
|
styleUrls: ['./app.component.css'],
|
|
514
142
|
})
|
|
515
143
|
export class AppComponent {
|
|
516
|
-
constructor(public auth: AuthService
|
|
517
|
-
|
|
518
|
-
loginWithRedirect(): void {
|
|
519
|
-
const { organization, invitation } = this.activatedRoute.snapshot.params;
|
|
144
|
+
constructor(public auth: AuthService) {}
|
|
520
145
|
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
invitation
|
|
524
|
-
});
|
|
146
|
+
loginWithRedirect() {
|
|
147
|
+
this.auth.loginWithRedirect();
|
|
525
148
|
}
|
|
526
|
-
}
|
|
527
|
-
```
|
|
528
|
-
|
|
529
|
-
## Angular Universal
|
|
530
|
-
|
|
531
|
-
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.
|
|
532
|
-
|
|
533
|
-
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.
|
|
534
|
-
|
|
535
|
-
## Development
|
|
536
|
-
|
|
537
|
-
### Build
|
|
538
|
-
|
|
539
|
-
Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.
|
|
540
|
-
|
|
541
|
-
### Running unit tests
|
|
542
|
-
|
|
543
|
-
Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io).
|
|
544
|
-
|
|
545
|
-
### Running end-to-end tests
|
|
546
|
-
|
|
547
|
-
The end-to-end tests are executed using [Cypress](https://www.cypress.io/) against the built-in playground app.
|
|
548
|
-
|
|
549
|
-
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:
|
|
550
|
-
|
|
551
|
-
```
|
|
552
|
-
CYPRESS_INTEGRATION_PASSWORD=<password> ng e2e
|
|
553
149
|
```
|
|
554
150
|
|
|
555
|
-
|
|
151
|
+
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.
|
|
556
152
|
|
|
557
|
-
|
|
153
|
+
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).
|
|
558
154
|
|
|
559
|
-
|
|
155
|
+
## API reference
|
|
560
156
|
|
|
561
|
-
|
|
562
|
-
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).
|
|
157
|
+
Explore public API's available in auth0-angular.
|
|
563
158
|
|
|
564
|
-
|
|
159
|
+
- [AuthService](https://auth0.github.io/auth0-angular/classes/auth_service.authservice.html) - service used to interact with the SDK.
|
|
160
|
+
- [AuthConfig](https://auth0.github.io/auth0-angular/interfaces/auth_config.authconfig.html) - used to configure the SDK.
|
|
565
161
|
|
|
566
|
-
##
|
|
162
|
+
## Feedback
|
|
567
163
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
## Contributing
|
|
164
|
+
### Contributing
|
|
571
165
|
|
|
572
166
|
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
|
|
573
167
|
|
|
574
168
|
- [Auth0's general contribution guidelines](https://github.com/auth0/open-source-template/blob/master/GENERAL-CONTRIBUTING.md)
|
|
575
169
|
- [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
|
|
576
170
|
|
|
577
|
-
|
|
171
|
+
### Raise an issue
|
|
578
172
|
|
|
579
|
-
|
|
173
|
+
To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-angular/issues).
|
|
580
174
|
|
|
581
|
-
|
|
175
|
+
### Vulnerability Reporting
|
|
582
176
|
|
|
583
177
|
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.
|
|
584
178
|
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
## License
|
|
599
|
-
|
|
600
|
-
This project is licensed under the MIT license. See the [LICENSE](https://github.com/auth0/auth0-angular/blob/master/LICENSE) file for more info.
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
<p align="center">
|
|
182
|
+
<picture>
|
|
183
|
+
<source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
184
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
|
|
185
|
+
<img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
186
|
+
</picture>
|
|
187
|
+
</p>
|
|
188
|
+
<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>
|
|
189
|
+
<p align="center">
|
|
190
|
+
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>
|