Installation
You have a few options for using the Auth0 SPA SDK in your project:- From the CDN:
<script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js"></script>
. For more information, read the FAQ. - With npm:
npm install @auth0/auth0-spa-js
- With yarn:
yarn add @auth0/auth0-spa-js
Getting Started
Create the client
First, you’ll need to create a new instance of theAuth0Client
client object. Create the Auth0Client
instance before rendering or initializing your application. You can do this using either the async/await method, or with promises. You should only create one instance of the client.
Using createAuth0Client
does a couple of things automatically:
- It creates an instance of
Auth0Client
. - It calls
getTokenSilently
to refresh the user session. - It suppresses all errors from
getTokenSilently
, exceptlogin_required
.
Use async/await
Use promises
Auth0Client
constructor. This can be useful if you want to:
- Bypass the call to
getTokenSilently
on initialization. - Do custom error handling.
- Initialize the SDK in a synchronous way.
Login and get user info
Next, create a button users can click to start logging in:<button id="login">Click to Login</button>
Listen for click events on the button you created. When the event occurs, use the desired login method to authenticate the user (loginWithRedirect()
in this example). After the user is authenticated, you can retrieve the user profile with the getUser()
method.
Use async/await
Use promises
Call an API
To call your API, start by getting the user’s . Then use the Access Token in your request. In this example, thegetTokenSilently
method is used to retrieve the Access Token:
<button id="callApi">Call an API</button>
Use async/await
Use promises
Logout
Add a button users can click to logout:<button id="logout">Logout</button>
Change storage options
The Auth0 SPA SDK stores tokens in memory by default. However, this does not provide persistence across page refreshes and browser tabs. Instead, you can opt-in to store tokens in local storage by setting thecacheLocation
property to localstorage
when initializing the SDK. This can help to mitigate some of the effects of browser privacy technology that prevents access to the Auth0 by storing Access Tokens for longer.
Storing tokens in browser local storage provides persistence across page refreshes and browser tabs. However, if an attacker can achieve running JavaScript in the SPA using a cross-site scripting (XSS) attack, they can retrieve the tokens stored in local storage. A vulnerability leading to a successful XSS attack can be either in the SPA source code or in any third-party JavaScript code (such as bootstrap, jQuery, or Google Analytics) included in the SPA.Read more about token storage.
Use rotating Refresh Tokens
The Auth0 SPA SDK can be configured to use rotating Refresh Tokens to get new access tokens silently. These can be used to bypass browser privacy technology that prevents access to the Auth0 session cookie when authenticating silently, as well as providing built-in reuse detection. Configure the SDK to do this by settinguseRefreshTokens
to true
on initialization:
offline_access
scope during the authorization step. Furthermore, getTokenSilently
will then call the /oauth/token
endpoint directly to exchange refresh tokens for access tokens.
The SDK will obey the storage configuration when storing refresh tokens. If the SDK has been configured using the default in-memory storage mechanism, refresh tokens will be lost when refreshing the page.
Usage
Below are examples of usage for various methods in the SDK. Note that jQuery is used in these examples.Login with redirect
Redirect to the/authorize
endpoint at Auth0, starting the Universal Login flow:
Login with popup
Use a popup window to log in using the page:error.popup.close
:
popup
option in the options
object:
Login with redirect callback
When the browser is redirected from Auth0 back to your SPA,handleRedirectCallback
must be called in order to complete the login flow:
Get Access Token with no interaction
Get a new Access Token silently using either a hidden iframe andprompt=none
, or by using a rotating Refresh Token. Refresh Tokens are used when useRefreshTokens
is set to true
when configuring the SDK.
Getting an Access Token silently without using Refresh Tokens will not work in browsers that block third-party cookies, such as Safari and Brave. To learn more about the custom domain workaround, read Troubleshoot Renew Tokens When Using Safari.
getTokenSilently()
method requires you to have Allow Skipping User Consent enabled in your API Settings in the Dashboard. Additionally, user consent cannot be skipped on ‘localhost’.
Get Access Token with popup
Access Tokens can also be retrieved using a popup window. UnlikegetTokenSilently
, this method of retrieving an Access Token will work in browsers where third-party cookies are blocked by default:
Get Access Token for a different audience
Options may be passed togetTokenSilently
that get an Access Token with a different and scope of that which was requested at user authentication time.
This only works when not using Refresh Tokens (
useRefreshTokens: false
), as a Refresh Token is bound to the particular audience and scope that was requested at user authentication time.Get user
You can get the authenticated user’s profile data by calling thegetUser
method:
Get ID Token claims
You can get the claims of the authenticated user’s by calling thegetIdTokenClaims
method:
Logout (default)
You can initiate a logout action by calling thelogout
method:
Logout with no client ID
You can initiate a logout action with no specified by calling thelogout
method and including clientId: null
: