Difficulty: Intermediate
Why do we use both access and refresh tokens? How should refresh be designed and stored securely?
Consider a hotel again. The room key card (access token) opens the door but stops working after a day. To get a fresh one you show ID at the front desk (refresh token), which is used rarely and guarded carefully. The design balances two competing needs: security wants credentials that expire quickly, so a stolen one is useful only briefly, while usability wants users to stay signed in for weeks without typing passwords.
An access token is presented on every API call. It is short lived, commonly 5 to 15 minutes, often a JWT verified locally. Because it travels constantly and sits in memory of clients and services, the exposure surface is large, so you keep its lifetime small and its scope narrow. A refresh token is a longer lived credential, days to months, that can only be sent to the authorization server's token endpoint to obtain new access tokens. It is used infrequently, so it has far fewer chances to leak. It is typically an opaque random string stored server side, so it can be revoked.
The flow: the user logs in, receives both. The client calls the API with the access token. When the API returns 401 due to expiry, or the client proactively notices exp, it calls POST /token with grant_type=refresh_token, and gets a new access token, and in a good design a new refresh token too.
Refresh token rotation is the important modern practice. Each use of a refresh token invalidates it and issues a new one. If an attacker steals a token and uses it, then the legitimate client's next attempt presents an already used token, which the server treats as reuse detection: it revokes the whole token family and forces re-login. Store a hash of refresh tokens, a family id and a last used time in the database, exactly like password hashing.
Storage on the client: for browsers, the safest common pattern is an HttpOnly, Secure, SameSite cookie for the refresh token, so JavaScript cannot read it and XSS cannot exfiltrate it, and keep the access token in memory only. Avoid localStorage for long lived tokens, since any XSS can steal it. Cookies bring CSRF considerations, mitigated by SameSite and by scoping the cookie path to the refresh endpoint. On mobile, use the platform Keystore or Keychain.
Additional design points: bind refresh tokens to a device or client; support explicit logout by deleting the server-side record; set an absolute maximum session lifetime, not just sliding expiry; allow users to see and revoke active sessions; and when a password changes or an account is suspended, revoke all refresh tokens. Access tokens will still live until expiry, which is exactly why they are kept short, or you can add a small denylist for emergencies.
Concurrency edge case: two browser tabs refresh simultaneously, and with rotation the second call fails as reuse. Solve it with a short grace window that permits the immediately previous token, or coordinate refresh through a single tab or a shared lock.
Why not just use one long lived token? Because you could not revoke it cheaply if it were a self-contained JWT, and a single leak would grant weeks of access. Why not just short ones with re-login? Because users would hate it.
POST /oauth/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&refresh_token=rt_9f8e7d&client_id=web
HTTP/1.1 200 OK
Cache-Control: no-store
Set-Cookie: refresh=rt_2c1b0a; HttpOnly; Secure; SameSite=Strict; Path=/oauth/token
{ "access_token": "eyJhbGciOi...", "token_type": "Bearer", "expires_in": 900 }
A new refresh token replaces the old one; the old value is now invalid and reuse of it triggers revocation of the token family.
access token, refresh token, rotation, token storage