Writing
Fullstack English

JWTs, Sessions, and the Refresh Token Rotation You Actually Need

Access tokens expire fast, refresh tokens rotate on every use, and a reuse means the family dies. Auth that survives a stolen token.

  • Published
  • 1 min read
On this page3 sections
  1. The setup that holds up
  2. Rotation with reuse detection
  3. Boring answers to loud questions

The JWT-versus-session debate misses the operational question: what happens when a token is stolen? Your architecture’s answer to that decides everything else.

The setup that holds up

  • Access token: JWT, 5-15 min lifetime, held in memory (not localStorage). Short enough that revocation lists are mostly unnecessary.
  • Refresh token: opaque random string, httpOnly + Secure cookie, stored hashed server-side, single use.

Rotation with reuse detection

Every refresh burns the old token and issues a new one - same family id:

text
on_refresh(presented):
  rec = find_by_hash(sha256(presented))
  if not rec: deny()
  if rec.spent:                      # replay: someone holds an old token
      invalidate_family(rec.family)  # every token issued from it dies
      require_login()
  spend(rec)
  return issue_access(), issue_refresh(family=rec.family)

A stolen refresh token now has a shelf life of one use. The moment both the attacker and the real user try to refresh, the family self-destructs and someone re-logs in. Damage: minutes, not months.

Boring answers to loud questions

  • Logout = revoke family. Password change = revoke all families.
  • Mobile apps: same scheme, tokens in Keystore/Keychain.
  • Don’t put roles in access tokens you cannot re-check server-side; the token is a hint, the database is the truth.