What are the best practices for storing tokens in web browsers? (2024)

Last updated on Sep 2, 2024

  1. All
  2. Digital Authentication

Powered by AI and the LinkedIn community

1

Use HTTPS

Be the first to add your personal experience

2

Choose the right storage location

Be the first to add your personal experience

3

Set expiration dates

Be the first to add your personal experience

4

Implement refresh tokens

5

Here’s what else to consider

Tokens are pieces of data that authenticate users and grant them access to protected resources on web applications. They are often used in conjunction with APIs, OAuth, and OpenID Connect protocols. But how do you store tokens securely in web browsers, where they can be vulnerable to theft, leakage, or misuse? In this article, we will explore some of the best practices for storing tokens in web browsers, such as using HTTPS, choosing the right storage location, setting expiration dates, and implementing refresh tokens.

Top experts in this article

Selected by the community from 2 contributions. Learn more

What are the best practices for storing tokens in web browsers? (1)

Earn a Community Top Voice badge

Add to collaborative articles to get recognized for your expertise on your profile. Learn more

  • Rafael Pereira Software Developer

    What are the best practices for storing tokens in web browsers? (3) 1

What are the best practices for storing tokens in web browsers? (4) What are the best practices for storing tokens in web browsers? (5) What are the best practices for storing tokens in web browsers? (6)

1 Use HTTPS

One of the most basic and essential practices for storing tokens in web browsers is to use HTTPS, or Hypertext Transfer Protocol Secure. HTTPS encrypts the communication between the browser and the server, preventing anyone from intercepting or tampering with the tokens. HTTPS also ensures that the browser is connecting to the legitimate and trusted server, not a malicious or spoofed one. To use HTTPS, you need to obtain a valid SSL certificate from a certificate authority and configure your server to serve HTTPS requests.

Add your perspective

Help others by sharing more (125 characters min.)

2 Choose the right storage location

Another important decision for storing tokens in web browsers is where to store them. There are three main options: cookies, local storage, and session storage. Each option has its own advantages and disadvantages, depending on your security and usability requirements. Cookies are small files that are sent by the server and stored by the browser. They can be accessed by both the server and the browser, and they can have expiration dates and flags to limit their scope and visibility. However, cookies are also susceptible to cross-site request forgery (CSRF) attacks, where a malicious site can trick the browser into sending a request with the cookie to the target site. Local storage and session storage are part of the Web Storage API, which allows the browser to store key-value pairs of data. Local storage persists across sessions, while session storage is cleared when the browser is closed. They can only be accessed by the browser, not the server, and they have more storage capacity than cookies. However, they are also vulnerable to cross-site scripting (XSS) attacks, where a malicious script can read or write the data in the storage.

Add your perspective

Help others by sharing more (125 characters min.)

3 Set expiration dates

A good practice for storing tokens in web browsers is to set expiration dates for them. Expiration dates limit the validity and lifespan of the tokens, reducing the risk of token reuse or compromise. Expiration dates can be set on both the server and the client side, depending on the type and format of the token. For example, JSON Web Tokens (JWTs) are self-contained tokens that encode the expiration date in their payload. The server can verify the expiration date when validating the token, and the browser can check the expiration date before sending the token. Alternatively, the server can maintain a list of valid tokens and revoke them when they expire or when the user logs out.

Add your perspective

Help others by sharing more (125 characters min.)

4 Implement refresh tokens

A common challenge for storing tokens in web browsers is how to handle token expiration and renewal. One solution is to implement refresh tokens, which are long-lived tokens that can be used to obtain new access tokens, which are short-lived tokens that grant access to resources. Refresh tokens are usually stored securely on the server side, while access tokens are stored on the browser side. When an access token expires, the browser can request a new one from the server using the refresh token. This way, the user does not have to re-authenticate every time, and the access tokens are refreshed frequently.

Add your perspective

Help others by sharing more (125 characters min.)

  • Carl Gieringer Software Engineer at Google
    • Report contribution

    "Refresh tokens are usually stored securely on the server side" this is technically true, but misleading. Clients must also store refresh tokens or else they couldn't send them to the server to obtain access tokens.Browsers should store refresh tokens in Secure HttpOnly cookies to prevent XSS. Server endpoints recognizing the refresh token must not have any side effects (other than generating and returning a new access token) to prevent CSRF. Clients must not make the access token accessible to cross-origin JavaScript: i.e. never store it somewhere accessible from the global `window` object, such as in `window.localStorage`. Store it in-memory only, and call the server to get a new one on app startup.

    Like

5 Here’s what else to consider

This is a space to share examples, stories, or insights that don’t fit into any of the previous sections. What else would you like to add?

Add your perspective

Help others by sharing more (125 characters min.)

  • Rafael Pereira Software Developer

    (edited)

    • Report contribution

    JWTs may not always be the optimal choice for your specific requirements. This could be due to the absence of built-in expiry revocation or the potential vulnerability of having a plaintext payload (If the information is too sensible), in which case storing it unencrypted at all may not be the best choice. In these instances Phantom Tokens could be a viable alternative, instead of implementing workarounds – your clients can store Opaque Tokens, and a dedicated Token Service can convert them into a previously generated JWT accessible exclusively to your back-end. This allows you to enjoy the benefits of a enhanced security, addressing the limitations posed by JWT's in certain scenarios (at the cost of statelessness.)

    Like

    What are the best practices for storing tokens in web browsers? (23) 1

Authentication What are the best practices for storing tokens in web browsers? (24)

Authentication

+ Follow

Rate this article

We created this article with the help of AI. What do you think of it?

It’s great It’s not so great

Thanks for your feedback

Your feedback is private. Like or react to bring the conversation to your network.

Tell us more

Report this article

More articles on Authentication

No more previous content

  • How do you deal with MFA and 2FA failures and recovery options?
  • How do you implement multi-factor authentication without annoying your users? 1 contribution
  • What are the benefits and challenges of using SAML for single sign-on (SSO)? 2 contributions
  • What are the common challenges and best practices of implementing MFA and 2FA?
  • How do you test and validate Kerberos and SSO functionality before and after a system upgrade or migration?
  • What are the benefits and challenges of implementing DMARC for email security?
  • How do you measure and improve the effectiveness of your MFA and 2FA policies? 3 contributions

No more next content

See all

More relevant reading

  • HTML5 How do you handle CORS and CSP compatibility across different browsers and devices?
  • Web Applications How can you ensure a seamless web application user experience across all platforms?
  • Programming How can you ensure secure code when working with cookies?
  • HTML How do you implement fallback solutions for local storage and cookies in older or unsupported browsers?

Are you sure you want to delete your contribution?

Are you sure you want to delete your reply?

What are the best practices for storing tokens in web browsers? (2024)
Top Articles
Starting to Invest in 2024? Here's How Much You'll Need to Become a Millionaire in 30 Years
Best Investment for Monthly Income in India
Ron Martin Realty Cam
Libiyi Sawsharpener
Botw Royal Guard
15 Types of Pancake Recipes from Across the Globe | EUROSPAR NI
Gw2 Legendary Amulet
Hair Love Salon Bradley Beach
Simpsons Tapped Out Road To Riches
Check From Po Box 1111 Charlotte Nc 28201
25Cc To Tbsp
Lazarillo De Tormes Summary and Study Guide | SuperSummary
50 Shades Of Grey Movie 123Movies
Closest Bj Near Me
Skip The Games Fairbanks Alaska
Busted Campbell County
Toyota Camry Hybrid Long Term Review: A Big Luxury Sedan With Hatchback Efficiency
Sea To Dallas Google Flights
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
Page 2383 – Christianity Today
Danielle Ranslow Obituary
Downtown Dispensary Promo Code
130Nm In Ft Lbs
UAE 2023 F&B Data Insights: Restaurant Population and Traffic Data
Motor Mounts
County Cricket Championship, day one - scores, radio commentary & live text
134 Paige St. Owego Ny
Opsahl Kostel Funeral Home & Crematory Yankton
Tributes flow for Soundgarden singer Chris Cornell as cause of death revealed
Joplin Pets Craigslist
Nacho Libre Baptized Gif
Reborn Rich Ep 12 Eng Sub
Henry County Illuminate
Rage Of Harrogath Bugged
Uc Santa Cruz Events
Gun Mayhem Watchdocumentaries
Tryst Houston Tx
Jasgotgass2
Clima De 10 Días Para 60120
Emily Tosta Butt
'Guys, you're just gonna have to deal with it': Ja Rule on women dominating modern rap, the lyrics he's 'ashamed' of, Ashanti, and his long-awaited comeback
Pekin Soccer Tournament
How Big Is 776 000 Acres On A Map
Gon Deer Forum
Germany’s intensely private and immensely wealthy Reimann family
Washington Craigslist Housing
Gummy Bear Hoco Proposal
Subdomain Finer
Electronics coupons, offers & promotions | The Los Angeles Times
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 6030

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.