Auth0 SDK for Single Page Applications using Authorization Code Grant Flow with PKCE.
Table of Contents
- Getting Started
- Support + Feedback
- Frequently Asked Questions
- Vulnerability Reporting
- What is Auth0
From the CDN:
npm install @auth0/auth0-spa-js
yarn add @auth0/auth0-spa-js
Creating the client
Auth0Client instance before rendering or initializing your application. You should only have one instance of the client.
;//with async/awaitconst auth0 = await;//with promises;//or, you can just instantiate the client on it's own;const auth0 =domain: '<AUTH0_DOMAIN>'client_id: '<AUTH0_CLIENT_ID>'redirect_uri: '<MY_CALLBACK_URL>';//if you do this, you'll need to check the session yourselftryawait ;catch errorif errorerror !== 'login_required'throw error;
1 - Login
Click to Login
//with async/await//redirect to the Universal Login Pagedocument;//in your callback route (<MY_CALLBACK_URL>)window;//with promises//redirect to the Universal Login Pagedocument;//in your callback route (<MY_CALLBACK_URL>)window;
2 - Calling an API
Call an API
//with async/awaitdocument;//with promisesdocument;
3 - Logout
Data caching options
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.
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:
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.
Refresh tokens can be used to request new access tokens. Read more about how our refresh tokens work for browser-based applications to help you decide whether or not you need to use them.
To enable the use of refresh tokens, set the
useRefreshTokens option to
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
/token endpoint directly. This means that in most cases the SDK does not rely on third-party cookies when using refresh tokens.
Note This configuration option requires Rotating Refresh Tokens to be enabled for your Auth0 Tenant.
Refresh Token fallback
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.
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.
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.
Advanced options can be set by specifying the
advancedOptions property when configuring
Auth0Client. Learn about the complete set of advanced options in the API documentation
We appreciate feedback and contribution to this repo! Before you get started, please see the following:
- Auth0's general contribution guidelines
- Auth0's code of conduct guidelines
- This repo's contribution guide
Support + Feedback
For support or to provide feedback, please raise an issue on our issue tracker.
Frequently Asked Questions
For a rundown of common issues you might encounter when using the SDK, please check out the FAQ.
Please do not report security vulnerabilities on the public GitHub issue tracker. The Responsible Disclosure Program details the procedure for disclosing security issues.
What is Auth0?
Auth0 helps you to easily:
- 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.)
- log in users with username/password databases, passwordless, or multi-factor authentication
- link multiple user accounts together
- generate signed JSON Web Tokens to authorize your API calls and flow the user identity securely
- access demographics and analytics detailing how, when, and where users are logging in
This project is licensed under the MIT license. See the LICENSE file for more info.