Track 07 · Lesson 5 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
Access and refresh tokens
Lesson 5 of 18· 7 min
A short-lived access token (minutes) is sent with every API call, so a leak is only useful briefly. A long-lived refresh token (days) is used only to get new access tokens, so users aren’t logged out every 15 minutes.
Let Mallory steal the refresh token
Use the API, let time pass, then attack
Access token
A1Valid for 15 more minutes. Kept in memory, sent as Authorization: Bearer A1.
Refresh token
R1Valid for 30 days. Lives in an HttpOnly cookie, only sent to /auth/refresh.
Two tokens, two jobs#
The access token is a bearer token: it works for whoever holds it, sent as Authorization: Bearer …. Keeping it to minutes limits what a leak is worth. The refresh token is only ever sent to one endpoint, POST /auth/refresh, and the server tracks it, so it can be revoked like a session.
Rotation and reuse detection#
With rotation, every refresh returns a new refresh token and retires the old one. If a retired token ever shows up again, two parties hold copies, so one of them stole it. The server can’t tell which, so it revokes the whole session family. The thief is locked out, and the real user just logs in again.
The window that stays open#
Logging out revokes the refresh token on the server, but any access token already issued keeps working until it expires. That window is the price of stateless tokens, and the reason to keep them short.
Check yourself