Track 07 · Lesson 6 of 18Identity and login

Track 07 · Identity and login

Where to keep tokens

Lesson 6 of 18· 8 min

Every place you can store a token trades one risk for another. Anything page JavaScript can read, an XSS bug can steal. Anything the browser sends automatically, a CSRF attack can ride on. Every pattern in this track is a different answer to those two risks.

Pick a home for each token

Watch what each attack can do

Access token lives in

  • An XSS script can steal it✕ Yes: one line reads it
  • A CSRF attack can use it✓ No: your code attaches it
  • Survives a page reload✓ Yes
  • Works across tabs✓ Yes

Refresh token lives in

  • An XSS script can steal it✕ Yes, and it stays useful for weeks
  • A CSRF attack can use it✓ No
Risky. A long-lived refresh token in localStorage is the jackpot for XSS: one stolen value means weeks of access.

The common, solid setup for single-page apps#

Access token in memory (a private variable, not storage), refresh token in a Secure, HttpOnly, SameSite=Strict cookie limited to the refresh endpoint. A reload wipes the access token, and the app silently gets a new one using the cookie. Even safer is a backend-for-frontend (BFF), where your own server keeps the tokens and the browser only holds a session cookie.

Three places with different trust#

Think of any web app’s auth as three zones: the browser (untrusted: any injected script runs here), your server (trusted: code you deploy), and the identity provider and API (the authority that decides what’s valid). Good designs keep the most valuable secret, usually the refresh token or session ID, out of the zone attackers can reach. Here’s where each secret lives in the five common setups.

Where does each secret live?

Pick a setup, open any item

A small server next to the frontend catches tokens at login, seals the refresh token in an HttpOnly cookie, and hands the browser only a short-lived access token kept in memory. Everyday API calls still go straight to the API. Strong protection with almost no backend changes.

Browser

Untrusted: any injected script runs here

Your server

Trusted: code you deploy

Identity & API

Authority: decides what's valid

Traffic paths in this setup

Auth traffic: Browser → BFF /api/auth/* → Auth server

Login, social sign-in, 2FA, refresh, logout. The browser talks only to its own origin; the BFF makes a server-to-server call. This is the only path where the refresh token exists, and it never comes back to the browser.

Everything else: Browser → API

Normal data calls go straight to the API with Authorization: Bearer <access token>. The BFF adds no latency to everyday use.

No storage choice survives XSS#

If an attacker gets script running on your page, they can do anything the user can do while the tab is open, whatever you chose. Good storage choices limit how long they keep access afterwards. What stops scripts from being injected is your framework’s escaping, sanitizing user HTML, and a strict Content Security Policy, so treat XSS prevention as the first auth defense.

Check yourself

Where should a long-lived refresh token live in a browser app?