@auth0/auth0-spa-js 2.0.0-beta.0 → 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.
Files changed (43) hide show
  1. package/README.md +45 -303
  2. package/dist/auth0-spa-js.development.js +73 -70
  3. package/dist/auth0-spa-js.development.js.map +1 -1
  4. package/dist/auth0-spa-js.production.esm.js +1 -1
  5. package/dist/auth0-spa-js.production.esm.js.map +1 -1
  6. package/dist/auth0-spa-js.production.js +1 -1
  7. package/dist/auth0-spa-js.production.js.map +1 -1
  8. package/dist/lib/auth0-spa-js.cjs.js +73 -70
  9. package/dist/lib/auth0-spa-js.cjs.js.map +1 -1
  10. package/dist/typings/Auth0Client.d.ts +3 -4
  11. package/dist/typings/Auth0Client.utils.d.ts +3 -1
  12. package/dist/typings/cache/cache-localstorage.d.ts +2 -2
  13. package/dist/typings/cache/cache-manager.d.ts +2 -2
  14. package/dist/typings/cache/key-manifest.d.ts +1 -1
  15. package/dist/typings/cache/shared.d.ts +3 -3
  16. package/dist/typings/global.d.ts +1 -1
  17. package/dist/typings/http.d.ts +1 -1
  18. package/dist/typings/promise-utils.d.ts +1 -1
  19. package/dist/typings/scope.d.ts +1 -1
  20. package/dist/typings/transaction-manager.d.ts +1 -1
  21. package/dist/typings/utils.d.ts +3 -3
  22. package/dist/typings/version.d.ts +1 -1
  23. package/dist/typings/worker/worker.types.d.ts +3 -3
  24. package/package.json +2 -2
  25. package/src/Auth0Client.ts +77 -82
  26. package/src/Auth0Client.utils.ts +8 -6
  27. package/src/cache/cache-localstorage.ts +2 -2
  28. package/src/cache/cache-manager.ts +18 -6
  29. package/src/cache/cache-memory.ts +2 -2
  30. package/src/cache/key-manifest.ts +1 -1
  31. package/src/cache/shared.ts +2 -2
  32. package/src/global.ts +1 -1
  33. package/src/http.ts +2 -2
  34. package/src/jwt.ts +19 -15
  35. package/src/promise-utils.ts +5 -2
  36. package/src/scope.ts +2 -2
  37. package/src/storage.ts +3 -3
  38. package/src/transaction-manager.ts +1 -1
  39. package/src/utils.ts +6 -3
  40. package/src/version.ts +1 -1
  41. package/src/worker/token.worker.ts +4 -4
  42. package/src/worker/worker.types.ts +3 -3
  43. package/src/worker/worker.utils.ts +1 -0
package/README.md CHANGED
@@ -1,57 +1,38 @@
1
- # @auth0/auth0-spa-js (Beta)
1
+ ![Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE.](https://cdn.auth0.com/website/sdks/banners/spa-js-banner.png)
2
2
 
3
- Auth0 SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce).
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
- ![Stage: Beta Release](https://img.shields.io/badge/stage-fa-yellow)
8
- [![CircleCI](https://circleci.com/gh/auth0/auth0-spa-js.svg?style=svg)](https://circleci.com/gh/auth0/auth0-spa-js)
9
- ![Release](https://img.shields.io/github/v/release/auth0/auth0-spa-js)
3
+ ![Release](https://img.shields.io/npm/v/@auth0/auth0-spa-js)
10
4
  [![Codecov](https://img.shields.io/codecov/c/github/auth0/auth0-spa-js)](https://codecov.io/gh/auth0/auth0-spa-js)
11
5
  ![Downloads](https://img.shields.io/npm/dw/@auth0/auth0-spa-js)
12
6
  [![License](https://img.shields.io/:license-mit-blue.svg?style=flat)](https://opensource.org/licenses/MIT)
7
+ ![CircleCI](https://img.shields.io/circleci/build/github/auth0/auth0-spa-js)
13
8
 
14
- ## Table of Contents
15
-
16
- - [Documentation](#documentation)
17
- - [Installation](#installation)
18
- - [Getting Started](#getting-started)
19
- - [Contributing](#contributing)
20
- - [Support + Feedback](#support--feedback)
21
- - [Frequently Asked Questions](#frequently-asked-questions)
22
- - [Vulnerability Reporting](#vulnerability-reporting)
23
- - [What is Auth0](#what-is-auth0)
24
- - [License](#license)
9
+ 📚 [Documentation](#documentation) - 🚀 [Getting Started](#getting-started) - 💻 [API Reference](#api-reference) - 💬 [Feedback](#feedback)
25
10
 
26
11
  ## Documentation
27
12
 
28
- - [Documentation](https://auth0.com/docs/libraries/auth0-spa-js)
29
- - [API reference](https://auth0.github.io/auth0-spa-js/)
30
- - [Migrate from Auth0.js to the Auth0 Single Page App SDK](https://auth0.com/docs/libraries/auth0-spa-js/migrate-from-auth0js)
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.
31
18
 
32
- ## Installation
33
-
34
- From the CDN:
19
+ ## Getting Started
35
20
 
36
- ```html
37
- <script src="https://cdn.auth0.com/js/auth0-spa-js/1.22/auth0-spa-js.production.js"></script>
38
- ```
21
+ ### Installation
39
22
 
40
- Using [npm](https://npmjs.org):
23
+ Using [npm](https://npmjs.org) in your project directory run the following command:
41
24
 
42
25
  ```sh
43
- npm install @auth0/auth0-spa-js@fa
26
+ npm install @auth0/auth0-spa-js
44
27
  ```
45
28
 
46
- Using [yarn](https://yarnpkg.com):
29
+ From the CDN:
47
30
 
48
- ```sh
49
- yarn add @auth0/auth0-spa-js@fa
31
+ ```html
32
+ <script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js"></script>
50
33
  ```
51
34
 
52
- ## Getting Started
53
-
54
- ### Auth0 Configuration
35
+ ### Configure Auth0
55
36
 
56
37
  Create a **Single Page Application** in the [Auth0 Dashboard](https://manage.auth0.com/#/applications).
57
38
 
@@ -73,7 +54,7 @@ Next, configure the following URLs for your application under the "Application U
73
54
 
74
55
  Take note of the **Client ID** and **Domain** values under the "Basic Information" section. You'll need these values in the next step.
75
56
 
76
- ### Creating the client
57
+ ### Configure the SDK
77
58
 
78
59
  Create an `Auth0Client` instance before rendering or initializing your application. You should only have one instance of the client.
79
60
 
@@ -89,17 +70,6 @@ const auth0 = await createAuth0Client({
89
70
  }
90
71
  });
91
72
 
92
- //with promises
93
- createAuth0Client({
94
- domain: '<AUTH0_DOMAIN>',
95
- clientId: '<AUTH0_CLIENT_ID>',
96
- authorizationParams: {
97
- redirect_uri: '<MY_CALLBACK_URL>'
98
- }
99
- }).then(auth0 => {
100
- //...
101
- });
102
-
103
73
  //or, you can just instantiate the client on it's own
104
74
  import { Auth0Client } from '@auth0/auth0-spa-js';
105
75
 
@@ -121,15 +91,15 @@ try {
121
91
  }
122
92
  ```
123
93
 
124
- ### 1 - Login
94
+ ### Logging In
95
+
96
+ You can then use login using the `Auth0Client` instance you created:
125
97
 
126
98
  ```html
127
99
  <button id="login">Click to Login</button>
128
100
  ```
129
101
 
130
102
  ```js
131
- //with async/await
132
-
133
103
  //redirect to the Universal Login Page
134
104
  document.getElementById('login').addEventListener('click', async () => {
135
105
  await auth0.loginWithRedirect();
@@ -142,245 +112,22 @@ window.addEventListener('load', async () => {
142
112
  const user = await auth0.getUser();
143
113
  console.log(user);
144
114
  });
145
-
146
- //with promises
147
-
148
- //redirect to the Universal Login Page
149
- document.getElementById('login').addEventListener('click', () => {
150
- auth0.loginWithRedirect().catch(() => {
151
- //error while redirecting the user
152
- });
153
- });
154
-
155
- //in your callback route (<MY_CALLBACK_URL>)
156
- window.addEventListener('load', () => {
157
- auth0.handleRedirectCallback().then(redirectResult => {
158
- //logged in. you can get the user profile like this:
159
- auth0.getUser().then(user => {
160
- console.log(user);
161
- });
162
- });
163
- });
164
- ```
165
-
166
- ### 2 - Calling an API
167
-
168
- ```html
169
- <button id="call-api">Call an API</button>
170
- ```
171
-
172
- ```js
173
- //with async/await
174
- document.getElementById('call-api').addEventListener('click', async () => {
175
- const accessToken = await auth0.getTokenSilently();
176
- const result = await fetch('https://myapi.com', {
177
- method: 'GET',
178
- headers: {
179
- Authorization: `Bearer ${accessToken}`
180
- }
181
- });
182
- const data = await result.json();
183
- console.log(data);
184
- });
185
-
186
- //with promises
187
- document.getElementById('call-api').addEventListener('click', () => {
188
- auth0
189
- .getTokenSilently()
190
- .then(accessToken =>
191
- fetch('https://myapi.com', {
192
- method: 'GET',
193
- headers: {
194
- Authorization: `Bearer ${accessToken}`
195
- }
196
- })
197
- )
198
- .then(result => result.json())
199
- .then(data => {
200
- console.log(data);
201
- });
202
- });
203
- ```
204
-
205
- ### 3 - Logout
206
-
207
- ```html
208
- <button id="logout">Logout</button>
209
- ```
210
-
211
- ```js
212
- import { createAuth0Client } from '@auth0/auth0-spa-js';
213
-
214
- document.getElementById('logout').addEventListener('click', () => {
215
- auth0.logout();
216
- });
217
115
  ```
218
116
 
219
- 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):
117
+ For other comprehensive examples, see the [EXAMPLES.md](https://github.com/auth0/auth0-spa-js/blob/master/EXAMPLES.md) document.
220
118
 
221
- ```js
222
- auth0.logout({
223
- returnTo: 'https://your.custom.url.example.com/'
224
- });
225
- ```
119
+ ## API Reference
226
120
 
227
- ### Data caching options
121
+ Explore API Methods available in auth0-spa-js.
228
122
 
229
- 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.
123
+ - [Configuration Options](https://auth0.github.io/auth0-spa-js/interfaces/auth0clientoptions.html)
230
124
 
231
- 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:
125
+ - [Auth0Client](https://auth0.github.io/auth0-spa-js/classes/auth0client.html)
126
+ - [createAuth0Client](https://auth0.github.io/auth0-spa-js/globals.html#createauth0client)
232
127
 
233
- ```js
234
- await createAuth0Client({
235
- domain: '<AUTH0_DOMAIN>',
236
- clientId: '<AUTH0_CLIENT_ID>',,
237
- cacheLocation: 'localstorage' // valid values are: 'memory' or 'localstorage',
238
- authorizationParams: {
239
- redirect_uri: '<MY_CALLBACK_URL>'
240
- }
241
- });
242
- ```
243
-
244
- **Important:** This feature will allow the caching of data **such as ID and access tokens** to be stored in local storage. Exercising this option changes the security characteristics of your application and **should not be used lightly**. Extra care should be taken to mitigate against XSS attacks and minimize the risk of tokens being stolen from local storage.
245
-
246
- #### Creating a custom cache
247
-
248
- The SDK can be configured to use a custom cache store that is implemented by your application. This is useful if you are using this SDK in an environment where more secure token storage is available, such as potentially a hybrid mobile app.
128
+ ## Feedback
249
129
 
250
- To do this, provide an object to the `cache` property of the SDK configuration.
251
-
252
- The object should implement the following functions. Note that all of these functions can optionally return a Promise or a static value.
253
-
254
- | Signature | Return type | Description |
255
- | -------------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
256
- | `get(key)` | Promise<object> or object | Returns the item from the cache with the specified key, or `undefined` if it was not found |
257
- | `set(key: string, object: any) ` | Promise<void> or void | Sets an item into the cache |
258
- | `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 |
259
- | `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@@` |
260
-
261
- Here's an example of a custom cache implementation that uses `sessionStorage` to store tokens and apply it to the Auth0 SPA SDK:
262
-
263
- ```js
264
- const sessionStorageCache = {
265
- get: function (key) {
266
- return JSON.parse(sessionStorage.getItem(key));
267
- },
268
-
269
- set: function (key, value) {
270
- sessionStorage.setItem(key, JSON.stringify(value));
271
- },
272
-
273
- remove: function (key) {
274
- sessionStorage.removeItem(key);
275
- },
276
-
277
- // Optional
278
- allKeys: function () {
279
- return Object.keys(sessionStorage);
280
- }
281
- };
282
-
283
- await createAuth0Client({
284
- domain: '<AUTH0_DOMAIN>',
285
- clientId: '<AUTH0_CLIENT_ID>',
286
- cache: sessionStorageCache,
287
- authorizationParams: {
288
- redirect_uri: '<MY_CALLBACK_URL>'
289
- }
290
- });
291
- ```
292
-
293
- **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.
294
-
295
- We also export the internal `InMemoryCache` and `LocalStorageCache` implementations, so you can wrap your custom cache around these implementations if you wish.
296
-
297
- ### Refresh Tokens
298
-
299
- 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.
300
-
301
- To enable the use of refresh tokens, set the `useRefreshTokens` option to `true`:
302
-
303
- ```js
304
- await createAuth0Client({
305
- domain: '<AUTH0_DOMAIN>',
306
- clientId: '<AUTH0_CLIENT_ID>',
307
- useRefreshTokens: true,
308
- authorizationParams: {
309
- redirect_uri: '<MY_CALLBACK_URL>'
310
- }
311
- });
312
- ```
313
-
314
- 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.
315
-
316
- **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).
317
-
318
- #### Refresh Token fallback
319
-
320
- 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.
321
-
322
- 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.
323
-
324
- **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.
325
-
326
- ### Organizations
327
-
328
- [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.
329
-
330
- #### Log in to an organization
331
-
332
- Log in to an organization by specifying the `organization` parameter when setting up the client:
333
-
334
- ```js
335
- createAuth0Client({
336
- domain: '<AUTH0_DOMAIN>',
337
- clientId: '<AUTH0_CLIENT_ID>',
338
- authorizationParams: {
339
- organization: '<MY_ORG_ID>',
340
- redirect_uri: '<MY_CALLBACK_URL>'
341
- }
342
- });
343
- ```
344
-
345
- You can also specify the organization when logging in:
346
-
347
- ```js
348
- // Using a redirect
349
- client.loginWithRedirect({
350
- authorizationParams: {
351
- organization: '<MY_ORG_ID>'
352
- }
353
- });
354
-
355
- // Using a popup window
356
- client.loginWithPopup({
357
- authorizationParams: {
358
- organization: '<MY_ORG_ID>'
359
- }
360
- });
361
- ```
362
-
363
- #### Accept user invitations
364
-
365
- 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.
366
-
367
- ```js
368
- const url = new URL(invitationUrl);
369
- const params = new URLSearchParams(url.search);
370
- const organization = params.get('organization');
371
- const invitation = params.get('invitation');
372
-
373
- if (organization && invitation) {
374
- client.loginWithRedirect({
375
- authorizationParams: {
376
- invitation,
377
- organization
378
- }
379
- });
380
- }
381
- ```
382
-
383
- ## Contributing
130
+ ### Contributing
384
131
 
385
132
  We appreciate feedback and contribution to this repo! Before you get started, please see the following:
386
133
 
@@ -388,31 +135,26 @@ We appreciate feedback and contribution to this repo! Before you get started, pl
388
135
  - [Auth0's code of conduct guidelines](https://github.com/auth0/open-source-template/blob/master/CODE-OF-CONDUCT.md)
389
136
  - [This repo's contribution guide](https://github.com/auth0/auth0-spa-js/blob/master/CONTRIBUTING.md)
390
137
 
391
- ## Support + Feedback
392
-
393
- For support or to provide feedback, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-spa-js/issues).
394
-
395
- ## Frequently Asked Questions
138
+ ### Raise an issue
396
139
 
397
- For a rundown of common issues you might encounter when using the SDK, please check out [the FAQ](https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md).
140
+ To provide feedback or report a bug, please [raise an issue on our issue tracker](https://github.com/auth0/auth0-spa-js/issues).
398
141
 
399
- ## Vulnerability Reporting
142
+ ### Vulnerability Reporting
400
143
 
401
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.
402
145
 
403
146
  ## What is Auth0?
404
147
 
405
- Auth0 helps you to easily:
406
-
407
- - implement authentication with multiple identity providers, including social (e.g., Google, Facebook, Microsoft, LinkedIn, GitHub, Twitter, etc), or enterprise (e.g., Windows Azure AD, Google Apps, Active Directory, ADFS, SAML, etc.)
408
- - log in users with username/password databases, passwordless, or multi-factor authentication
409
- - link multiple user accounts together
410
- - generate signed JSON Web Tokens to authorize your API calls and flow the user identity securely
411
- - access demographics and analytics detailing how, when, and where users are logging in
412
- - enrich user profiles from other data sources using customizable JavaScript rules
413
-
414
- [Why Auth0?](https://auth0.com/why-auth0)
415
-
416
- ## License
417
-
418
- 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>