curly{bits}

curlybits / Guides / Where to store a JWT in the browser: cookie or localStorage?

Where to store a JWT in the browser: cookie or localStorage?

Short answer: for a website, keep the token out of JavaScript's reach in an HttpOnly cookie, or keep it in memory and refresh it from one. Here's why, and how to set it up.

First: a JWT isn't secret, but it is a key

Here's a real signed token:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1c2VyXzQyIiwibmFtZSI6IkF1bmciLCJyb2xlIjoiZWRpdG9yIiwiaWF0IjoxNzk1MDAwMDAwLCJleHAiOjE3OTUwMDM2MDB9.Osw3JIgA5K2n9V1W9gTf4kpsjm8sJi3hh5ub_Q8Aq8U

Paste it into the JWT decoder and anyone can read the middle part:

{ "sub": "user_42", "name": "Aung", "role": "editor", "iat": 1795000000, "exp": 1795003600 }

The payload is only Base64URL-encoded, not encrypted, so never put passwords or private data in it. What the signature protects is changes: edit the role to admin without knowing the secret, and the signature no longer matches. But whoever holds the whole token can use it until it expires. So the question isn't “can someone read it?” but “can someone steal it?”.

The two attacks that matter

  • XSS (cross-site scripting): attacker-controlled JavaScript runs on your page, from an injection bug or a compromised third-party script. It can read anything JavaScript can read, and send it anywhere.
  • CSRF (cross-site request forgery): another site makes the user's browser send a request to your site. The browser attaches your cookies automatically, so the request arrives logged in, even though the attacker never sees the cookie.

Every storage option trades one of these against the other.

The options compared

WhereReadable by XSS?Sent automatically (CSRF)?Survives a reload?
localStorageYesNoYes, until removed
sessionStorageYesNoOnly in that tab
A JavaScript variable (memory)Harder, but XSS can still call your API while the page is openNoNo
HttpOnly cookieNoYes, so set SameSiteYes, until it expires

With localStorage, a single XSS bug lets an attacker copy the token and use it from their own computer for as long as it's valid. With an HttpOnly cookie, the same bug can still make requests while the user has the page open, but it can't take the token away. That's a smaller, shorter-lived problem, which is why most security guidance prefers cookies for browser sessions.

A safe default for websites

Let the server set the token in a cookie like this:

Set-Cookie: __Host-session=eyJhbGciOi…; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=3600
  • HttpOnly: JavaScript can't read it, so XSS can't steal it.
  • Secure: only sent over HTTPS.
  • SameSite=Lax: not sent on most cross-site requests, such as forms posted from another site, which blocks the usual CSRF attack. Use Strict if users never arrive from links on other sites already logged in. Chrome and Edge treat cookies with no SameSite as Lax, but not every browser does, so always set it.
  • __Host- prefix: the browser only accepts the cookie if it has Secure and Path=/ and no Domain, so a subdomain can't overwrite it.
  • Max-Age no longer than the token's own exp.

Then the front end doesn't handle the token at all. Same-site requests send it automatically; for an API on another origin, use fetch(url, { credentials: 'include' }) and allow credentials in the API's CORS settings.

For changes made with POST, PUT or DELETE, also check the Origin header on the server, or use a CSRF token, as a second layer behind SameSite.

Single-page apps calling a separate API

If cookies won't work for your setup, the common pattern is:

  1. Keep a short-lived access token (5 to 15 minutes) in a JavaScript variable only, never in storage.
  2. Keep a refresh token in an HttpOnly, Secure, SameSite cookie scoped to the refresh endpoint (Path=/auth/refresh).
  3. When the page loads, or the access token expires, call the refresh endpoint to get a new one. Rotate the refresh token each time and revoke the old one, so a stolen refresh token stops working once it's used twice.

When localStorage is acceptable

For a low-risk internal tool, a prototype, or a token with very limited scope and a lifetime of minutes, localStorage can be a reasonable trade-off. Reduce the damage: short expiry, a strict Content Security Policy, few third-party scripts, and a way to revoke tokens on the server.

Checklist

  • Nothing private in the payload: decode your own token and look.
  • Short exp on access tokens; refresh instead of issuing long-lived ones.
  • Verify the signature on every request, and reject alg: none and unexpected algorithms.
  • Sign HS256 tokens with a long random secret, at least 32 bytes (a random string generator will make one), never a word or a reused password.
  • Cookie flags: HttpOnly; Secure; SameSite=Lax or Strict, and the __Host- prefix where you can.

How this guide was checked: every example on this page was run as written, using Node.js 22 (the token, signature and decoding examples). Spotted a mistake? Tell us and we'll fix it.

Updated