Where to keep a JWT in a browser: memory, storage or cookie
A JWT in a browser is a bearer credential: whoever has the string can use it until it expires. The storage choice decides who can get the string, and what an attacker needs to do to use it. There is no option that is safe against everything; there are trade-offs that fit different architectures.
The three common places are JavaScript memory, Web Storage and cookies. They fail in different ways. A useful way to evaluate any proposal is to ask two questions: can a cross-site request cause an authenticated action, and can a script running on the page read the credential? The answers decide whether you need CSRF protection, XSS containment, or both.
| Storage | Readable by script | Sent automatically | Main risk |
|---|---|---|---|
| JS variable | Yes, while the page lives | No, must add header | XSS reads it; lost on reload |
| sessionStorage | Yes | No | XSS reads it; per-tab |
| localStorage | Yes | No | XSS reads it; persists |
| HttpOnly cookie | No | Yes, same site rules | CSRF; XSS can still act |
| BFF session cookie | No | Yes | CSRF; server holds tokens |
Memory and Web Storage
Keeping the access token in a closure or a module variable is the least exposed JavaScript option. The token is not readable by another tab and not persisted, so a stolen laptop or a shared browser does not leave a credential behind. The cost is that a reload loses it, which means a refresh flow on every page load, often via a refresh token in an HttpOnly cookie. If the refresh token is in a cookie, the CSRF considerations apply to the refresh endpoint.
localStorage and sessionStorage are convenient and widely used, and they are readable by any script on the origin. A single XSS, including one in a third-party analytics or chat widget, can read and exfiltrate every token stored there. sessionStorage limits the lifetime to the tab but not the exposure. If you use either, treat XSS prevention as the primary control: a strict Content-Security-Policy, no inline scripts, dependency review, and output encoding everywhere.
Cookies and CSRF
An HttpOnly cookie cannot be read by JavaScript, which removes the simplest token theft. The browser sends it automatically, which is convenient and also the problem: a request triggered by another site can carry the cookie. SameSite=Lax blocks most cross-site POSTs and is a sensible default; SameSite=Strict is stronger but can break links from external sites. For state-changing requests, pair SameSite with a CSRF token or an origin check on the server.
Use the Secure attribute so the cookie is only sent over HTTPS, scope it with Path and __Host- where possible, and keep the cookie small. A JWT with many claims can exceed the 4 KB cookie limit, especially when several cookies share the domain. If the token is large, a BFF that keeps it server-side is a better fit than a bigger cookie.
The backend-for-frontend pattern
In a BFF, the browser talks only to a server component on the same origin. That component completes the OAuth flow, stores the access and refresh tokens server-side keyed by a session, and sets an HttpOnly session cookie. API calls go through the BFF, which attaches the access token. The browser never holds a token, so XSS cannot steal one, and the token format can change without touching the front end.
The BFF adds a server, a session store and a hop of latency, and it must protect the session cookie like any other session: rotation on login, expiry, CSRF defence, and revocation. In exchange it removes token storage from the browser and centralises refresh, revocation and audience handling. For many business applications that is the right trade.
Refresh tokens need more care than access tokens
A refresh token is long-lived and can mint new access tokens, so it deserves stronger protection than the access token. Keep it in an HttpOnly, Secure, SameSite cookie or server-side, never in localStorage. Rotate it on every use, detect reuse, and bind it to the client. For public clients, use PKCE on the authorization flow and sender-constraining where the platform supports it.
A common mistake is to store the refresh token in localStorage and the access token in memory, believing the short access lifetime limits the damage. The refresh token is the more valuable target and now sits in the most exposed place. If the refresh token must live in the browser, the honest assessment is that XSS equals full account takeover for the session lifetime.
A decision guide
- Server-rendered app with a backend? Use a BFF session cookie and keep tokens server-side.
- SPA with a backend you control? Prefer BFF; if not possible, access token in memory and refresh in an HttpOnly cookie.
- SPA with no backend? Accept that XSS is the dominant risk and invest in CSP, dependency control and short lifetimes.
- Mobile app? Use the platform's secure storage and the authorization-code flow with PKCE.
- Any cookie that authenticates a request? Set Secure, HttpOnly, SameSite and add CSRF protection for state changes.
Whatever you choose, write the threat model down. A table that says where each token lives, who can read it, how long it lasts and how it is revoked is more useful than a debate about localStorage. The verifier on this site deliberately stores nothing, which is one end of the spectrum: no persistence, no theft from disk, and a reload clears everything.
There is no storage location that makes a bearer token safe by itself. The surrounding controls — CSP, CSRF defence, short lifetimes, revocation — do most of the work.
Sources
- OWASP HTML5 security cheat sheet — storage
- OWASP CSRF prevention cheat sheet
- RFC 9700 §4.1 — Token storage and replay
Questions
Is localStorage safe for JWT?
It is readable by any JavaScript running on the origin, so one XSS bug exposes every token stored there. That does not make it always wrong, but it means the security of the token equals the security of the whole front end, including every third-party script.
If I use an HttpOnly cookie, am I safe from XSS?
No. HttpOnly stops JavaScript from reading the cookie, but an XSS bug can still send authenticated requests from the victim's browser. It also introduces CSRF unless SameSite and, ideally, a CSRF token are used. HttpOnly moves the risk, it does not remove it.
What is the BFF pattern?
Backend for frontend: the browser holds only an HttpOnly session cookie, and a server-side component holds the access and refresh tokens and calls the APIs. The browser never sees a token, so XSS cannot steal one, and CSRF is handled with SameSite and a CSRF token.
This page stores no token at all — reload the verifier and everything is gone.
Open the verifierWritten against the JOSE specifications (RFC 7515–7519), RFC 7636 and RFC 8037; every example can be reproduced in the verifier linked above.
Back to the guides