An access token is proof that a user may call an API for a while. Where you keep it decides which bug can steal it. localStorage, a JavaScript variable, and an HttpOnly cookie fail in different ways. There is no universal safest box. There is a sensible default for a normal first-party website, and there are other designs when the API is called from a browser that cannot use your cookie.
The login dance itself is a separate article from cookie names. Cookie flags and prefixes are in cookie prefixes for session cookies and HttpOnly, Secure, and SameSite. Keys that were never supposed to reach the browser are in frontend API keys. This page is only about the token you issued for the signed-in user.
Three places, three failures
A variable in memory disappears when the tab closes, and every script on the page can read it if your code can. localStorage survives reloads, which is why teams like it, and any script on the origin can read it too. A cross-site scripting bug becomes a token theft bug the moment the token is in a place JavaScript can see. An HttpOnly cookie is not readable from JavaScript. The browser attaches it to requests for your host. A cross-site request forgery bug can then cause the browser to send it. SameSite, a CSRF token, and a same-origin API all exist because of that trade.
Session storage is localStorage with a shorter life. It is not a security boundary. Encoding the token, or splitting it across two keys, is not a boundary either. If the page can reassemble it, a script on the page can reassemble it.
The usual default for a first-party site
If the browser and the API share a site you control, keep the session in an HttpOnly cookie. Set Secure so it only moves over HTTPS. Set SameSite explicitly. Lax fits many ordinary sites. Strict fits when you do not need the cookie on a cross-site navigation into the app. None is for a cookie that must be sent cross-site, and it requires Secure. Prefer a host-only cookie name, as the prefix article describes, unless a subdomain architecture truly needs a wider Domain attribute.
Keep the cookie’s lifetime short, and rotate the server-side session on login. A cookie that lasts a year is a stolen laptop problem. A cookie that lasts an hour, with a refresh path you can revoke, is a smaller one. The refresh token belongs in the same kind of cookie, or in the server, not in localStorage “because it is only a refresh token.” It is the longer-lived secret.
When the token has to live in JavaScript
A mobile app, a third-party frontend on another domain, or a CLI is not your first-party cookie story. Those clients often hold a bearer token and send it in an Authorization header. That can be an acceptable design when the client is not a random script on a website, and when the token is short-lived, audience-restricted, and revocable. Putting that same bearer token in localStorage on a marketing site that loads tag managers is how a compromised tag becomes an account takeover.
If a single-page app on your own domain calls your API, you still have the cookie option. Choosing a bearer token in memory is reasonable when you are defending specifically against CSRF and you accept that an XSS bug can read the token until the tab closes. Choosing localStorage so the user stays signed in after a refresh accepts a longer theft window. Write that choice down. Do not arrive there because a tutorial used localStorage.
A backend-for-frontend is an architecture
A backend-for-frontend is a small server your browser calls, which holds the upstream token and sets a normal session cookie for the browser. It is not a third storage location inside the browser. The browser still has a cookie or it has nothing. The upstream token stays on the server. This is the right shape when the upstream identity provider issues a token you do not want in JavaScript, and when your pages do not need to call that provider directly.
The extra server must check the user’s session before it uses the upstream token. An open proxy that attaches your token to whatever URL the browser names is a new bug, not a pattern. Keep the allowlist of upstream calls short.
What a scan can see
A public header and cookie check can see that a cookie lacks HttpOnly or Secure, or that SameSite is missing. The security headers analyzer and a website scan’s cookie notes are that evidence. They cannot see a token in localStorage, because that value is not in the HTTP response they are judging as a cookie. They cannot see a memory variable. If the login response returns a JSON token and the page stores it, the finding may be “sensitive token in a script-readable response,” which a human still has to trace into the storage call.
The JWT decoder shows you the claims in a token you paste locally. It does not prove the token was stored safely, and it does not validate a signature by itself. Decoding is not the storage decision.
Pick one design and test the failure
For a first-party session, start with the HttpOnly cookie and an explicit SameSite value. Add CSRF protection on state-changing requests if anything about your flow still sends the cookie cross-site. If you instead keep a bearer token in the page, keep it in memory, keep it short, and treat every XSS bug as token theft. Do not put it in localStorage unless you have accepted that longer window in writing.
Test logout. The cookie should be cleared or the server session destroyed so the old value stops working. A frontend that deletes a localStorage key and leaves the server token valid has not logged the user out. Retest on the real host, not only in a local build, because a gateway can rewrite Set-Cookie and drop HttpOnly on the way out.
Check the cookie the browser actually stored
Open the real site after login and read the cookie flags in the browser's own storage panel. Confirm HttpOnly, Secure, and SameSite are present on the session cookie, and that no second cookie holds the same token without HttpOnly. A framework default and a gateway rewrite can disagree. The browser is the referee. If a token also appears in a script-readable response body, the cookie work is incomplete until that body stops carrying it.
Repeat the check on the www host and the apex host if both serve the app. A cookie set on one and missing on the other is how a fixed login still looks broken, or still looks wide open, depending on which host the customer typed.
Frequently asked questions
Is localStorage safe if the token is a JWT?
No. The format does not matter. If JavaScript can read it, an XSS bug can read it. HttpOnly is a cookie attribute, not a JWT feature.
Does SameSite replace a CSRF token?
SameSite reduces some cross-site sends. It is not a complete story for every login flow, especially where you had to use None. Keep a CSRF control that matches how the cookie is actually sent.
Which option should we ship this week?
If the app and the API are yours, ship an HttpOnly, Secure cookie with an explicit SameSite and a short life. Revisit bearer tokens only when a client cannot use that cookie.
