Track 07 · Lesson 10 of 18Identity and login

Track 07 · Identity and login

Dead or just unlucky? Refresh decisions

Lesson 10 of 18· 6 min

The single most important rule in any refresh code: only a dead session logs you out. A transient failure (server down, rate limited, network blip) must keep the session, or every short outage signs out every user. This is one of the most common auth bugs in production.

The refresh decision table

Pick what the auth server answers

The auth server answers…

Auth server answered: 200 + tokens

Your server returns
200 { accessToken } (refresh token stripped)
Cookie
✓ Resealed with rotated RT
Browser result
{ ok: true }
Verdict
✓ Refreshed
User sees
Nothing visible. New token in memory, pushed to other tabs.

Why: The happy path.

Dead means definitive#

A refresh token is dead when the auth server says so: 400 invalid_grant (the OAuth standard error for an expired, revoked or reused refresh token, which arrives with 400, not 401), a 401, or an account that’s been disabled. A missing or tampered cookie is also “no session”. Normalize every 4xx except 429 to “dead” for the browser.

Everything else is transient#

429 Too many requests, any 5xx, a timeout, or an unexpected body like a CDN’s HTML error page: keep the cookie, keep the user signed in, and let a later request retry. Handle 429 before any generic 4xx rule, or a burst of reloads signs people out.

A contract for your refresh function#

Give the client’s refresh function three possible answers, and make every caller respect them:

{ ok: true }                       // new token is ready
{ ok: false, sessionDead: true }   // definitive no: log out
{ ok: false, sessionDead: false }  // anything else: keep session, retry later

Check yourself

During refresh the auth server answers 503. What should happen to the user?