Overview
Key Concepts
- How you decide to store your token is crucial to defending your application against malicious attacks.
- Review scenarios for each application type.
- Decide which method best supports your technology.
Next.js static site scenarios
When you’re building a Next.js application, authentication might be needed in the following cases:- When accessing a page
- When accessing an API route
- When your application calls an API hosted outside of your Next.js application on behalf of the user
- The user is redirected to Auth0.
- When the user is successfully signed in, they will be redirected back to the application.
-
The client-side will complete the code exchange with Auth0 and retrieve the user’s
id_token
andaccess_token
which will be stored in memory.
If your app is using a sign in scenario that doesn’t require API calls, only an ID token is required. There is no need to store it. You can validate it and get the data from it that you required.If your app needs to call APIs on behalf of the user, access tokens and (optionally) refresh tokens are needed. These can be stored server-side or in a session cookie. The cookie needs to be encrypted and have a maximum size of 4 KB. If the data to be stored is large, storing tokens in the session cookie is not a viable option.Use the following flow types in these scenarios:
Browser in-memory scenarios
Auth0 recommends storing tokens in browser memory as the most secure option. Using Web Workers to handle the transmission and storage of tokens is the best way to protect the tokens, as Web Workers run in a separate global scope than the rest of the application. Use Auth0 SPA SDK whose default storage option is in-memory storage leveraging Web Workers. If you cannot use Web Workers, Auth0 recommends as an alternative that you use JavaScript closures to emulate private methods. Use Auth0 SPA SDK whose default storage option is in-memory storage to leverage both Web Workers and JavaScript closures depending on the type of token.The in-memory method for browser storage does not provide persistence across page refreshes and browser tabs.
Browser local storage scenarios
Using browser local storage can be a viable alternative to mechanisms that require retrieving the from an iframe and to cookie-based authentication across domains when these are not possible due to browser restrictions (for example, ITP2).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.