Token Storage (2024)

Securing SPAs that make API calls come with their own set of concerns. You'll need to ensure that tokens and other sensitive data are not vulnerable to cross-site scripting (XSS) and can't be read by malicious JavaScript.

To learn more, see JWT Handbook and The Ultimate Guide to Next.js Authentication with Auth0.

Next.js static site scenarios

When you're building a Next.js application, authentication might be needed in the following cases:

  1. When accessing a page

  2. When accessing an API route

  3. When your application calls an API hosted outside of your Next.js application on behalf of the user

Where a server is available, your app can handle the interaction with Auth0 and create a session, but in this model, we don't have a backend. All of the work happens on the frontend:

  1. The user is redirected to Auth0.

  2. When the user is successfully signed in, they will be redirected back to the application.

  3. The client-side will complete the code exchange with Auth0 and retrieve the user's id_token and access_token which will be stored in memory.

    Token Storage (1)

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 access token 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.

To reduce security risks if your SPA is using implicit (we recommend using authorization code flow with PKCE instead) or hybrid flows, you can reduce the absolute token expiration time. This reduces the impact of a reflected XSS attack (but not of a persistent one). To reduce the expiration time, go to Dashboard > APIs > Settings > Token Expiration For Browser Flows (Seconds).

Reduce the amount of third-party JavaScript code included from a source outside your domain to the minimum needed (such as links to jQuery, Bootstrap, Google Analytics etc.) Reducing third-party JS code reduces the possibility of an XSS vulnerability. Performing Subresource Integrity (SRI) checking in third-party scripts (where possible) to verify that the resources fetched are delivered without unexpected manipulation is also more secure.

Learn more

Token Storage (2024)

FAQs

What is the best way to store tokens? ›

Applications can use dedicated APIs, such as the Web Storage API or IndexedDB, to store tokens. Applications can also simply keep the token in memory or put them in cookies. Some storage mechanisms are persistent, and others are wiped after some period of time or when the page is closed or refreshed.

What is a storage token? ›

Storage tokens refer to cryptocurrency tokens that aim to provide decentralised and secure storage solutions. These tokens leverage blockchain technology to offer a decentralised alternative to traditional storage solutions, such as cloud storage.

Should I store token in DB? ›

It is best to store the token in the 'users' table in the database. This is the most secure and efficient way to store the token. You can create a column in the 'users' table to store the token.

Is it secure to store token in local storage? ›

On the downside, localStorage is potentially vulnerable to cross-site scripting (XSS) attacks. If an attacker can inject malicious JavaScript into a webpage, they can steal an access token in localStorage. Also, unlike cookies, localStorage doesn't provide secure attributes that you can set to block attacks.

What is the safest way to store ERC20 tokens? ›

The most secure way to store ERC20 tokens is in cold storage. Options include a hardware device like Trezor or a paper wallet.

How long should a token be valid? ›

Give tokens an expiration: Technically, once a token is signed, it is valid forever—unless the signing key is changed or expiration explicitly set. This could pose potential issues so have a strategy for expiring and/or revoking tokens.

What are the 2 types of crypto storage? ›

Cryptocurrency Wallet Types. There are two main types of wallets: custodial and noncustodial. Custodial wallets (also called online wallets) are hosted by a third party that stores your keys for you. This could be a company that provides enterprise-level data security systems businesses use to preserve and secure data.

Why shouldn't you use localStorage? ›

Synchronous Blocking Operations: localStorage is not asynchronous and will block the main thread. It may even make your animations choppy depending on how much you read/ write and at what frequency. Asynchronicity is fundamental to creating fluid applications, especially for mobile devices.

Is it safe to store tokens in cookies? ›

The cookie attributes ensure that the browser only adds cookies to HTTPS requests, ensuring they are secure in transit. Since tokens are encrypted, they are also secured at rest. Tokens are then used to get secure access to APIs.

Where should I store ID token? ›

The id token can be safely sent to the browser or client and stored in a relatively insecure location, such as localstorage. The id token should never be used to access protected data, but instead is for displaying information about a user such as their name.

Where to store token after login? ›

Web Storage (local storage/session storage)

This token can then be stored in localStorage or sessionStorage. Both localStorage and sessionStorage both extend Storage . The only difference between them is the persistance of the data: localStorage - data persists until explicitly deleted.

Where should I store access token in front end? ›

There are three common options: local storage, session storage, and cookies. Each one has its pros and cons, but none of them is completely safe from attacks. Local storage and session storage are vulnerable to cross-site scripting (XSS) attacks, where malicious scripts can access and steal your tokens.

What is the most secure way to store tokens? ›

We recommend storing tokens on the server, as this offers traditional web apps the maximum level of security. If this cannot be done, you should use encrypted session cookies so the client cannot read token values.

Why is localStorage not secure? ›

Data stored in localStorage is easily accessible through browser developer tools, making it vulnerable to cross-site scripting (XSS) attacks. Malicious scripts injected into a website can easily access and manipulate data stored in localStorage , compromising sensitive user information.

Can tokens be stored in digital wallets? ›

Digital wallet – (e.g., Apple Pay or Google Wallet) requests and stores tokens for payment cards. Digital wallets undergo certification in order to utilize network tokenization services, allowing them to request and make purchases with tokens. Cardholder – owns the card that will be or has been tokenized.

What is the best way to store coins? ›

For high-value coins, use hard plastic holders. Professional coin grading services use sealed holders called slabs to protect authenticated and graded coins. Use acid-free cardboard and plastic holders free from polyvinyl chloride (PVC). Acid and PVC can ruin a coin's surface.

Where do we store tokens in front end? ›

There are three common options: local storage, session storage, and cookies. Each one has its pros and cons, but none of them is completely safe from attacks. Local storage and session storage are vulnerable to cross-site scripting (XSS) attacks, where malicious scripts can access and steal your tokens.

Top Articles
Was ist Professionelles Trading? | So arbeiten Profi Trader
What Are Flexible Benefits, and Should I Offer Them?
NYT Mini Crossword today: puzzle answers for Tuesday, September 17 | Digital Trends
Robot or human?
Cad Calls Meriden Ct
Usborne Links
Coffman Memorial Union | U of M Bookstores
Wmu Course Offerings
Craigslist - Pets for Sale or Adoption in Zeeland, MI
Craigslist Cars And Trucks Buffalo Ny
Parks in Wien gesperrt
Learn How to Use X (formerly Twitter) in 15 Minutes or Less
State Of Illinois Comptroller Salary Database
Matthew Rotuno Johnson
Sivir Urf Runes
Jang Urdu Today
Azpeople View Paycheck/W2
Viha Email Login
Barber Gym Quantico Hours
Lakewood Campground Golf Cart Rental
Ups Drop Off Newton Ks
How to Watch Every NFL Football Game on a Streaming Service
Fleet Farm Brainerd Mn Hours
Snohomish Hairmasters
4Oxfun
Star Wars Armada Wikia
Remnants of Filth: Yuwu (Novel) Vol. 4
Sam's Club Gas Price Hilliard
Sf Bay Area Craigslist Com
Average weekly earnings in Great Britain
O'reilly Auto Parts Ozark Distribution Center Stockton Photos
Slv Fed Routing Number
Joplin Pets Craigslist
October 31St Weather
450 Miles Away From Me
Hindilinks4U Bollywood Action Movies
“Los nuevos desafíos socioculturales” Identidad, Educación, Mujeres Científicas, Política y Sustentabilidad
Craigslist Farm And Garden Reading Pa
Dragon Ball Super Super Hero 123Movies
Big Reactors Best Coolant
Lyons Hr Prism Login
Hillsborough County Florida Recorder Of Deeds
News & Events | Pi Recordings
Hdmovie2 Sbs
Minterns German Shepherds
Zadruga Elita 7 Live - Zadruga Elita 8 Uživo HD Emitirani Sat Putem Interneta
10 Bedroom Airbnb Kissimmee Fl
Spongebob Meme Pic
Epower Raley's
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 5573

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.