Skip to the page

Authentication and authorization · senior

Access and refresh tokens, rotation and storage

Use a short-lived access token for API calls and a long-lived refresh token to get new ones; rotate the refresh token on every use and treat reuse of an old one as theft, revoking the whole token family.

The notes

What you must know

  • Access token: minutes (5-15), sent on every API call, usually a JWT. Refresh token: days or weeks, only sent to the token endpoint, stored server-side (as a hash) so it can be revoked.
  • Rotation: every refresh returns a new refresh token and invalidates the old one. If an old one is used again, someone copied it: revoke the whole family and force login. RFC 9700 requires rotation or sender-constrained tokens for public clients (SPAs, mobile apps).
  • Web storage: keep the refresh token in an HttpOnly, Secure, SameSite cookie scoped to the refresh path, and the access token in memory; or use a BFF so tokens never reach the browser. OWASP advises against putting tokens in localStorage (any XSS can read them).
  • Mobile: store refresh tokens in the Keychain (iOS) or Keystore-backed storage (Android), never AsyncStorage.
  • Client side: one refresh at a time (share the in-flight refresh promise across parallel 401s), then retry the failed requests; handle refresh failure by logging out.
  • Logout everywhere = revoke all refresh tokens for the user; access tokens die when they expire (keep them short).

The red pen

Common traps: mistake → fix

  • Mistake: A long-lived access token is simpler

    Fix: a stolen one works for its whole life and cannot be revoked easily.

  • Mistake: Refresh tokens can live in localStorage if access tokens are short

    Fix: XSS can steal the refresh token and mint access tokens for weeks.

  • Mistake: Let every failed request trigger its own refresh

    Fix: parallel refreshes race and rotation will flag them as reuse.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why have both access and refresh tokens?
  2. Where do you store each one in a SPA? In React Native?
  3. What is refresh token rotation and reuse detection?
  4. Ten requests get 401 at the same moment. What should the client do?
  5. How do you implement 'log out from all devices'?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview