@auth0/auth0-spa-js 2.0.0-beta.1 → 2.0.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 +45 -305
- package/dist/auth0-spa-js.development.js +1 -1
- package/dist/auth0-spa-js.development.js.map +1 -1
- package/dist/auth0-spa-js.production.esm.js +1 -1
- package/dist/auth0-spa-js.production.esm.js.map +1 -1
- package/dist/auth0-spa-js.production.js +1 -1
- package/dist/auth0-spa-js.production.js.map +1 -1
- package/dist/lib/auth0-spa-js.cjs.js +1 -1
- package/dist/lib/auth0-spa-js.cjs.js.map +1 -1
- package/dist/typings/version.d.ts +1 -1
- package/package.json +1 -1
- package/src/version.ts +1 -1
package/README.md
CHANGED
|
@@ -1,59 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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://circleci.com/gh/auth0/auth0-spa-js)
|
|
9
|
-

|
|
3
|
+

|
|
10
4
|
[](https://codecov.io/gh/auth0/auth0-spa-js)
|
|
11
5
|

|
|
12
6
|
[](https://opensource.org/licenses/MIT)
|
|
7
|
+

|
|
13
8
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
## Table of Contents
|
|
17
|
-
|
|
18
|
-
- [Documentation](#documentation)
|
|
19
|
-
- [Installation](#installation)
|
|
20
|
-
- [Getting Started](#getting-started)
|
|
21
|
-
- [Contributing](#contributing)
|
|
22
|
-
- [Support + Feedback](#support--feedback)
|
|
23
|
-
- [Frequently Asked Questions](#frequently-asked-questions)
|
|
24
|
-
- [Vulnerability Reporting](#vulnerability-reporting)
|
|
25
|
-
- [What is Auth0](#what-is-auth0)
|
|
26
|
-
- [License](#license)
|
|
9
|
+
📚 [Documentation](#documentation) - 🚀 [Getting Started](#getting-started) - 💻 [API Reference](#api-reference) - 💬 [Feedback](#feedback)
|
|
27
10
|
|
|
28
11
|
## Documentation
|
|
29
12
|
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
|
|
34
|
-
|
|
13
|
+
- [Quickstart](https://auth0.com/docs/quickstart/spa/vanillajs/interactive) - our interactive guide for quickly adding login, logout and user information to your app using Auth0.
|
|
14
|
+
- [Sample app](https://github.com/auth0-samples/auth0-javascript-samples/tree/master/01-Login) - a full-fledged sample app integrated with Auth0.
|
|
15
|
+
- [FAQs](https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md) - frequently asked questions about auth0-spa-js SDK.
|
|
16
|
+
- [Examples](https://github.com/auth0/auth0-spa-js/blob/master/EXAMPLES.md) - code samples for common scenarios.
|
|
17
|
+
- [Docs Site](https://auth0.com/docs) - explore our Docs site and learn more about Auth0.
|
|
35
18
|
|
|
36
|
-
|
|
19
|
+
## Getting Started
|
|
37
20
|
|
|
38
|
-
|
|
39
|
-
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.22/auth0-spa-js.production.js"></script>
|
|
40
|
-
```
|
|
21
|
+
### Installation
|
|
41
22
|
|
|
42
|
-
Using [npm](https://npmjs.org):
|
|
23
|
+
Using [npm](https://npmjs.org) in your project directory run the following command:
|
|
43
24
|
|
|
44
25
|
```sh
|
|
45
|
-
npm install @auth0/auth0-spa-js
|
|
26
|
+
npm install @auth0/auth0-spa-js
|
|
46
27
|
```
|
|
47
28
|
|
|
48
|
-
|
|
29
|
+
From the CDN:
|
|
49
30
|
|
|
50
|
-
```
|
|
51
|
-
|
|
31
|
+
```html
|
|
32
|
+
<script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js"></script>
|
|
52
33
|
```
|
|
53
34
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
### Auth0 Configuration
|
|
35
|
+
### Configure Auth0
|
|
57
36
|
|
|
58
37
|
Create a **Single Page Application** in the [Auth0 Dashboard](https://manage.auth0.com/#/applications).
|
|
59
38
|
|
|
@@ -75,7 +54,7 @@ Next, configure the following URLs for your application under the "Application U
|
|
|
75
54
|
|
|
76
55
|
Take note of the **Client ID** and **Domain** values under the "Basic Information" section. You'll need these values in the next step.
|
|
77
56
|
|
|
78
|
-
###
|
|
57
|
+
### Configure the SDK
|
|
79
58
|
|
|
80
59
|
Create an `Auth0Client` instance before rendering or initializing your application. You should only have one instance of the client.
|
|
81
60
|
|
|
@@ -91,17 +70,6 @@ const auth0 = await createAuth0Client({
|
|
|
91
70
|
}
|
|
92
71
|
});
|
|
93
72
|
|
|
94
|
-
//with promises
|
|
95
|
-
createAuth0Client({
|
|
96
|
-
domain: '<AUTH0_DOMAIN>',
|
|
97
|
-
clientId: '<AUTH0_CLIENT_ID>',
|
|
98
|
-
authorizationParams: {
|
|
99
|
-
redirect_uri: '<MY_CALLBACK_URL>'
|
|
100
|
-
}
|
|
101
|
-
}).then(auth0 => {
|
|
102
|
-
//...
|
|
103
|
-
});
|
|
104
|
-
|
|
105
73
|
//or, you can just instantiate the client on it's own
|
|
106
74
|
import { Auth0Client } from '@auth0/auth0-spa-js';
|
|
107
75
|
|
|
@@ -123,15 +91,15 @@ try {
|
|
|
123
91
|
}
|
|
124
92
|
```
|
|
125
93
|
|
|
126
|
-
###
|
|
94
|
+
### Logging In
|
|
95
|
+
|
|
96
|
+
You can then use login using the `Auth0Client` instance you created:
|
|
127
97
|
|
|
128
98
|
```html
|
|
129
99
|
<button id="login">Click to Login</button>
|
|
130
100
|
```
|
|
131
101
|
|
|
132
102
|
```js
|
|
133
|
-
//with async/await
|
|
134
|
-
|
|
135
103
|
//redirect to the Universal Login Page
|
|
136
104
|
document.getElementById('login').addEventListener('click', async () => {
|
|
137
105
|
await auth0.loginWithRedirect();
|
|
@@ -144,245 +112,22 @@ window.addEventListener('load', async () => {
|
|
|
144
112
|
const user = await auth0.getUser();
|
|
145
113
|
console.log(user);
|
|
146
114
|
});
|
|
147
|
-
|
|
148
|
-
//with promises
|
|
149
|
-
|
|
150
|
-
//redirect to the Universal Login Page
|
|
151
|
-
document.getElementById('login').addEventListener('click', () => {
|
|
152
|
-
auth0.loginWithRedirect().catch(() => {
|
|
153
|
-
//error while redirecting the user
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
//in your callback route (<MY_CALLBACK_URL>)
|
|
158
|
-
window.addEventListener('load', () => {
|
|
159
|
-
auth0.handleRedirectCallback().then(redirectResult => {
|
|
160
|
-
//logged in. you can get the user profile like this:
|
|
161
|
-
auth0.getUser().then(user => {
|
|
162
|
-
console.log(user);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
### 2 - Calling an API
|
|
169
|
-
|
|
170
|
-
```html
|
|
171
|
-
<button id="call-api">Call an API</button>
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
```js
|
|
175
|
-
//with async/await
|
|
176
|
-
document.getElementById('call-api').addEventListener('click', async () => {
|
|
177
|
-
const accessToken = await auth0.getTokenSilently();
|
|
178
|
-
const result = await fetch('https://myapi.com', {
|
|
179
|
-
method: 'GET',
|
|
180
|
-
headers: {
|
|
181
|
-
Authorization: `Bearer ${accessToken}`
|
|
182
|
-
}
|
|
183
|
-
});
|
|
184
|
-
const data = await result.json();
|
|
185
|
-
console.log(data);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
//with promises
|
|
189
|
-
document.getElementById('call-api').addEventListener('click', () => {
|
|
190
|
-
auth0
|
|
191
|
-
.getTokenSilently()
|
|
192
|
-
.then(accessToken =>
|
|
193
|
-
fetch('https://myapi.com', {
|
|
194
|
-
method: 'GET',
|
|
195
|
-
headers: {
|
|
196
|
-
Authorization: `Bearer ${accessToken}`
|
|
197
|
-
}
|
|
198
|
-
})
|
|
199
|
-
)
|
|
200
|
-
.then(result => result.json())
|
|
201
|
-
.then(data => {
|
|
202
|
-
console.log(data);
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
```
|
|
206
|
-
|
|
207
|
-
### 3 - Logout
|
|
208
|
-
|
|
209
|
-
```html
|
|
210
|
-
<button id="logout">Logout</button>
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
```js
|
|
214
|
-
import { createAuth0Client } from '@auth0/auth0-spa-js';
|
|
215
|
-
|
|
216
|
-
document.getElementById('logout').addEventListener('click', () => {
|
|
217
|
-
auth0.logout();
|
|
218
|
-
});
|
|
219
|
-
```
|
|
220
|
-
|
|
221
|
-
You can redirect users back to your app after logging out. This URL must appear in the **Allowed Logout URLs** setting for the app in your [Auth0 Dashboard](https://manage.auth0.com):
|
|
222
|
-
|
|
223
|
-
```js
|
|
224
|
-
auth0.logout({
|
|
225
|
-
returnTo: 'https://your.custom.url.example.com/'
|
|
226
|
-
});
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
### Data caching options
|
|
230
|
-
|
|
231
|
-
The SDK can be configured to cache ID tokens and access tokens either in memory or in local storage. The default is in memory. This setting can be controlled using the `cacheLocation` option when creating the Auth0 client.
|
|
232
|
-
|
|
233
|
-
To use the in-memory mode, no additional options need are required as this is the default setting. To configure the SDK to cache data using local storage, set `cacheLocation` as follows:
|
|
234
|
-
|
|
235
|
-
```js
|
|
236
|
-
await createAuth0Client({
|
|
237
|
-
domain: '<AUTH0_DOMAIN>',
|
|
238
|
-
clientId: '<AUTH0_CLIENT_ID>',,
|
|
239
|
-
cacheLocation: 'localstorage' // valid values are: 'memory' or 'localstorage',
|
|
240
|
-
authorizationParams: {
|
|
241
|
-
redirect_uri: '<MY_CALLBACK_URL>'
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
115
|
```
|
|
245
116
|
|
|
246
|
-
|
|
117
|
+
For other comprehensive examples, see the [EXAMPLES.md](https://github.com/auth0/auth0-spa-js/blob/master/EXAMPLES.md) document.
|
|
247
118
|
|
|
248
|
-
|
|
119
|
+
## API Reference
|
|
249
120
|
|
|
250
|
-
|
|
121
|
+
Explore API Methods available in auth0-spa-js.
|
|
251
122
|
|
|
252
|
-
|
|
123
|
+
- [Configuration Options](https://auth0.github.io/auth0-spa-js/interfaces/auth0clientoptions.html)
|
|
253
124
|
|
|
254
|
-
|
|
125
|
+
- [Auth0Client](https://auth0.github.io/auth0-spa-js/classes/auth0client.html)
|
|
126
|
+
- [createAuth0Client](https://auth0.github.io/auth0-spa-js/globals.html#createauth0client)
|
|
255
127
|
|
|
256
|
-
|
|
257
|
-
| -------------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
258
|
-
| `get(key)` | Promise<object> or object | Returns the item from the cache with the specified key, or `undefined` if it was not found |
|
|
259
|
-
| `set(key: string, object: any) ` | Promise<void> or void | Sets an item into the cache |
|
|
260
|
-
| `remove(key)` | Promise<void> or void | Removes a single item from the cache at the specified key, or no-op if the item was not found |
|
|
261
|
-
| `allKeys()` | Promise<string[]> or string [] | (optional) Implement this if your cache has the ability to return a list of all keys. Otherwise, the SDK internally records its own key manifest using your cache. **Note**: if you only want to ensure you only return keys used by this SDK, the keys we use are prefixed with `@@auth0spajs@@` |
|
|
128
|
+
## Feedback
|
|
262
129
|
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
```js
|
|
266
|
-
const sessionStorageCache = {
|
|
267
|
-
get: function (key) {
|
|
268
|
-
return JSON.parse(sessionStorage.getItem(key));
|
|
269
|
-
},
|
|
270
|
-
|
|
271
|
-
set: function (key, value) {
|
|
272
|
-
sessionStorage.setItem(key, JSON.stringify(value));
|
|
273
|
-
},
|
|
274
|
-
|
|
275
|
-
remove: function (key) {
|
|
276
|
-
sessionStorage.removeItem(key);
|
|
277
|
-
},
|
|
278
|
-
|
|
279
|
-
// Optional
|
|
280
|
-
allKeys: function () {
|
|
281
|
-
return Object.keys(sessionStorage);
|
|
282
|
-
}
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
await createAuth0Client({
|
|
286
|
-
domain: '<AUTH0_DOMAIN>',
|
|
287
|
-
clientId: '<AUTH0_CLIENT_ID>',
|
|
288
|
-
cache: sessionStorageCache,
|
|
289
|
-
authorizationParams: {
|
|
290
|
-
redirect_uri: '<MY_CALLBACK_URL>'
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
**Note:** The `cache` property takes precedence over the `cacheLocation` property if both are set. A warning is displayed in the console if this scenario occurs.
|
|
296
|
-
|
|
297
|
-
We also export the internal `InMemoryCache` and `LocalStorageCache` implementations, so you can wrap your custom cache around these implementations if you wish.
|
|
298
|
-
|
|
299
|
-
### Refresh Tokens
|
|
300
|
-
|
|
301
|
-
Refresh tokens can be used to request new access tokens. [Read more about how our refresh tokens work for browser-based applications](https://auth0.com/docs/tokens/concepts/refresh-token-rotation) to help you decide whether or not you need to use them.
|
|
302
|
-
|
|
303
|
-
To enable the use of refresh tokens, set the `useRefreshTokens` option to `true`:
|
|
304
|
-
|
|
305
|
-
```js
|
|
306
|
-
await createAuth0Client({
|
|
307
|
-
domain: '<AUTH0_DOMAIN>',
|
|
308
|
-
clientId: '<AUTH0_CLIENT_ID>',
|
|
309
|
-
useRefreshTokens: true,
|
|
310
|
-
authorizationParams: {
|
|
311
|
-
redirect_uri: '<MY_CALLBACK_URL>'
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
Using this setting will cause the SDK to automatically send the `offline_access` scope to the authorization server. Refresh tokens will then be used to exchange for new access tokens instead of using a hidden iframe, and calls the `/oauth/token` endpoint directly. This means that in most cases the SDK does not rely on third-party cookies when using refresh tokens.
|
|
317
|
-
|
|
318
|
-
**Note** This configuration option requires Rotating Refresh Tokens to be [enabled for your Auth0 Tenant](https://auth0.com/docs/tokens/guides/configure-refresh-token-rotation).
|
|
319
|
-
|
|
320
|
-
#### Refresh Token fallback
|
|
321
|
-
|
|
322
|
-
In all cases where a refresh token is not available, the SDK falls back to the legacy technique of using a hidden iframe with `prompt=none` to try and get a new access token and refresh token. This scenario would occur for example if you are using the in-memory cache and you have refreshed the page. In this case, any refresh token that was stored previously would be lost.
|
|
323
|
-
|
|
324
|
-
If the fallback mechanism fails, a `login_required` error will be thrown and could be handled in order to put the user back through the authentication process.
|
|
325
|
-
|
|
326
|
-
**Note**: This fallback mechanism does still require access to the Auth0 session cookie, so if third-party cookies are being blocked then this fallback will not work and the user must re-authenticate in order to get a new refresh token.
|
|
327
|
-
|
|
328
|
-
### Organizations
|
|
329
|
-
|
|
330
|
-
[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.
|
|
331
|
-
|
|
332
|
-
#### Log in to an organization
|
|
333
|
-
|
|
334
|
-
Log in to an organization by specifying the `organization` parameter when setting up the client:
|
|
335
|
-
|
|
336
|
-
```js
|
|
337
|
-
createAuth0Client({
|
|
338
|
-
domain: '<AUTH0_DOMAIN>',
|
|
339
|
-
clientId: '<AUTH0_CLIENT_ID>',
|
|
340
|
-
authorizationParams: {
|
|
341
|
-
organization: '<MY_ORG_ID>',
|
|
342
|
-
redirect_uri: '<MY_CALLBACK_URL>'
|
|
343
|
-
}
|
|
344
|
-
});
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
You can also specify the organization when logging in:
|
|
348
|
-
|
|
349
|
-
```js
|
|
350
|
-
// Using a redirect
|
|
351
|
-
client.loginWithRedirect({
|
|
352
|
-
authorizationParams: {
|
|
353
|
-
organization: '<MY_ORG_ID>'
|
|
354
|
-
}
|
|
355
|
-
});
|
|
356
|
-
|
|
357
|
-
// Using a popup window
|
|
358
|
-
client.loginWithPopup({
|
|
359
|
-
authorizationParams: {
|
|
360
|
-
organization: '<MY_ORG_ID>'
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
#### Accept user invitations
|
|
366
|
-
|
|
367
|
-
Accept a user invitation through the SDK by creating a route within your application that can handle the user invitation URL, and log the user in by passing the `organization` and `invitation` parameters from this URL. You can either use `loginWithRedirect` or `loginWithPopup` as needed.
|
|
368
|
-
|
|
369
|
-
```js
|
|
370
|
-
const url = new URL(invitationUrl);
|
|
371
|
-
const params = new URLSearchParams(url.search);
|
|
372
|
-
const organization = params.get('organization');
|
|
373
|
-
const invitation = params.get('invitation');
|
|
374
|
-
|
|
375
|
-
if (organization && invitation) {
|
|
376
|
-
client.loginWithRedirect({
|
|
377
|
-
authorizationParams: {
|
|
378
|
-
invitation,
|
|
379
|
-
organization
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
}
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
## Contributing
|
|
130
|
+
### Contributing
|
|
386
131
|
|
|
387
132
|
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
|
|
388
133
|
|
|
@@ -390,31 +135,26 @@ We appreciate feedback and contribution to this repo! Before you get started, pl
|
|
|
390
135
|
- [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
|
|
391
136
|
- [This repo's contribution guide](https://github.com/auth0/auth0-spa-js/blob/master/CONTRIBUTING.md)
|
|
392
137
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
For support or to provide feedback, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-spa-js/issues).
|
|
396
|
-
|
|
397
|
-
## Frequently Asked Questions
|
|
138
|
+
### Raise an issue
|
|
398
139
|
|
|
399
|
-
|
|
140
|
+
To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-spa-js/issues).
|
|
400
141
|
|
|
401
|
-
|
|
142
|
+
### Vulnerability Reporting
|
|
402
143
|
|
|
403
144
|
Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.
|
|
404
145
|
|
|
405
146
|
## What is Auth0?
|
|
406
147
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
This project is licensed under the MIT license. See the [LICENSE](https://github.com/auth0/auth0-spa-js/blob/master/LICENSE) file for more info.
|
|
148
|
+
<p align="center">
|
|
149
|
+
<picture>
|
|
150
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_dark_mode.png" width="150">
|
|
151
|
+
<source media="(prefers-color-scheme: light)" srcset="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
152
|
+
<img alt="Auth0 Logo" src="https://cdn.auth0.com/website/sdks/logos/auth0_light_mode.png" width="150">
|
|
153
|
+
</picture>
|
|
154
|
+
</p>
|
|
155
|
+
<p align="center">
|
|
156
|
+
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>
|
|
157
|
+
</p>
|
|
158
|
+
<p align="center">
|
|
159
|
+
This project is licensed under the MIT license. See the <a href="https://github.com/auth0/auth0-spa-js/blob/master/LICENSE"> LICENSE</a> file for more info.
|
|
160
|
+
</p>
|
|
@@ -472,7 +472,7 @@
|
|
|
472
472
|
exports.default = SuperTokensLock;
|
|
473
473
|
}));
|
|
474
474
|
var Lock = unwrapExports(browserTabsLock);
|
|
475
|
-
var version = "2.0.0
|
|
475
|
+
var version = "2.0.0";
|
|
476
476
|
const DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS = 60;
|
|
477
477
|
const DEFAULT_POPUP_CONFIG_OPTIONS = {
|
|
478
478
|
timeoutInSeconds: DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS
|