Refresh Token Rotation — Auth

Refresh token rotation: every POST /auth/refresh marks the old refresh token as used (the used_at column) and issues a new token in the same family. So a…

At 07:45, the School Portal server receives the refresh token R1. The problem is, Siti exchanged R1 half an hour ago. So who just sent it?

That question can only be asked when the server uses refresh token rotation. You know it from the Token Refresh Flow lesson: every time a refresh token is used, the server gives a new refresh token and kills the old one. So a refresh token is good for one exchange only. Picture a monthly parking ticket that is swapped every time you leave the gate. The old ticket is stamped "used", and you get a new one with the next number. If a stamped ticket appears at the gate again, the attendant knows a copy exists. He can't tell who the real owner is, so the whole series of tickets is cancelled, from the first to the newest. On the server, the ticket is the refresh token, the "used" stamp is the used_at column, the series of tickets is a family (a chain of tokens), and the attendant is POST /auth/refresh. What's new in this lesson is the family and how the server detects a token that is used again.

One refresh, one new token

The Token Refresh Flow lesson built the basics: a 15-minute access token in memory, a 30-day refresh token in an HttpOnly cookie, and POST /auth/refresh to exchange it. This lesson looks at the table on the server that makes rotation checkable. Each refresh token is one row in the refresh_tokens table:

Column Holds Why
token_hash SHA-256 of the token the real token is never stored
user_id its owner
family_id the id of the chain of tokens from one login to revoke one chain at once
expires_at when it expires 30 days after it is issued
used_at when it was exchanged empty means not used yet
revoked_at when it was revoked empty means not revoked yet

A family is every refresh token born from one login: R1 from the login, R2 from exchanging R1, R3 from exchanging R2, and so on. A new login starts a new family.

One refresh changes two rows: the token that was sent gets a used_at value, and a new token is born in the same family. A token whose used_at is already filled in can't be exchanged again.

An old token appears again

Siti logs in on Friday, 9 October 2026, at 07:00. This is what the server records, exactly as in the Lab:

Time Request Answer What changes in the table
07:00 POST /auth/login 200, A1 and R1 R1 is born in family F1
07:15 POST /auth/refresh, cookie R1 200, A2 and R2 R1 gets used_at 07:15
07:30 POST /auth/refresh, cookie R2 200, A3 and R3 R2 gets used_at 07:30
07:45 POST /auth/refresh, cookie R1 401 R1 was already used: R1, R2, R3 get revoked_at 07:45
08:00 POST /auth/refresh, cookie R3 (Siti) 401 R3 is revoked, Siti logs in again and gets family F2

From the server's side, R1 at 07:45 means two parties hold the same chain of tokens. Siti exchanged R1 at 07:15, so whoever sends R1 now holds a copy. The server can't tell which one is real. The safe move: revoke the whole family, including the R3 Siti is holding. Siti only has to log in again with her password. Whoever holds the copy can't do that.

Many servers choose a stricter way: revoke all of that user's refresh tokens, on every device, and send a notification email. At the very least, the family is revoked.

The answer to the client stays a plain 401 with the usual message, {"message": "Unauthenticated."}. The details, such as "reuse detected, family F1 revoked" (reuse means being used again), go to the server log, the record of what happens on the server. Those details don't go into the answer (the response).

One thing is not revoked: access tokens already issued. The server doesn't record access tokens, so each one stays valid until it expires, 15 minutes at most. That's why they are kept short.

The server code

This is the simplified server the Lab uses, in JavaScript. now is the time in minutes since Friday 07:00, and tokens are written short (R1, R2) so they are easy to follow. store is a Map (a collection of key and value pairs) that plays the part of the refresh_tokens table:

const DAY = 24 * 60;
const store = new Map(); // token -> { userId, familyId, expiresAt, usedAt, revokedAt }

function issue(userId, familyId, now) {
  const token = "R" + (store.size + 1); // really 32 random bytes, and the database stores only its hash
  store.set(token, { userId, familyId, expiresAt: now + 30 * DAY, usedAt: null, revokedAt: null });
  return token;
}

function revokeFamily(familyId, now) {
  for (const entry of store.values()) {
    if (entry.familyId === familyId && entry.revokedAt === null) entry.revokedAt = now;
  }
}

function refresh(token, now) {
  const entry = store.get(token);
  if (!entry || entry.revokedAt !== null || entry.expiresAt <= now) return { status: 401 };
  if (entry.usedAt !== null) {
    revokeFamily(entry.familyId, now); // used twice: revoke the whole family
    return { status: 401, reuse: true };
  }
  entry.usedAt = now;
  return { status: 200, refreshToken: issue(entry.userId, entry.familyId, now) };
}

The exercise: the family is every token of that user

The exercise is a smaller version. In the exercise, store is a Map whose key is the token string and whose value is { userId, used, valid }. There's no family_id column, so in this exercise the family is every entry (one key and value pair) with the same userId. rotateTokens(refreshToken, store) has four branches, checked in order:

Token state What it does What it returns
not in store changes nothing { success: false, error: "invalid" }
valid: false (already revoked) changes nothing { success: false, error: "revoked" }
used: true valid: false on every entry with the same userId { success: false, error: "reuse_detected" }
not used yet used: true, then a new entry { success: true, newAccessToken, newRefreshToken }

The new token names are "access_" + userId + "_" + (store.size + 1) and "refresh_" + userId + "_" + (store.size + 1). store.size is the number of entries in store, and it is calculated before the new entry is added. The store starts with one token, so the first rotation of refresh_user42_1 produces refresh_user42_2. The new entry is { userId, used: false, valid: true }, for the same user. So when refresh_user42_1 is used again, refresh_user42_2, which the client is holding, also becomes valid: false, and exchanging it is answered revoked. Another user's tokens are untouched.

📌 In Laravel

The /auth/refresh endpoint from the Token Refresh Flow lesson gets one new branch for reuse:

$token = RefreshToken::where('token_hash', hash('sha256', (string) $request->cookie('refresh_token')))->first();
abort_if($token === null || $token->revoked_at !== null || $token->expires_at->isPast(), 401, 'Unauthenticated.');

if ($token->used_at !== null) {
    RefreshToken::where('family_id', $token->family_id)->whereNull('revoked_at')->update(['revoked_at' => now()]);
    Log::warning('refresh_token.reuse', ['user_id' => $token->user_id, 'family_id' => $token->family_id]);
    abort(401, 'Unauthenticated.');
}

// atomic claim: of two requests at the same moment, only one changes this row
$claimed = RefreshToken::whereKey($token->id)->whereNull('used_at')->update(['used_at' => now()]);
abort_if($claimed !== 1, 401, 'Unauthenticated.');
$plain = Str::random(64);
$token->user->refreshTokens()->create([
    'family_id' => $token->family_id, // the new token stays in the same family
    'token_hash' => hash('sha256', $plain),
    'expires_at' => now()->addDays(30),
]);

⚠️ Leaks, false alarms and half-done revocation

TL;DR: Refresh token rotation: every POST /auth/refresh marks the old refresh token as used (the used_at column) and issues a new token in the same family. So a refresh token can be exchanged only once. A family is every refresh token from one login. When a used token appears again, there are two holders: revoke the whole family (or all of the user's tokens), answer a plain 401, and write the details to the server log (the record of what happens on the server). The server's refresh token table stores token_hash, user_id, family_id, expires_at, used_at, and revoked_at. Access tokens already issued stay valid until they expire, which is why they are short. In the exercise: invalid for an unknown token, reuse_detected plus valid: false on all of that user's tokens for reuse, and new token names built with store.size + 1 (the number of stored tokens plus one).

⚡ One refresh, one new token

Meaning: With refresh token rotation, every time a refresh token is used, the server fills in used_at (when it was exchanged) on the old token, then issues a new refresh token in the same family. A family is every refresh token that comes from one login. The old token dies at once, so a refresh token can be exchanged only once.

⚡ A used token appears again

Guess first: Siti logs in at 07:00 and gets R1. She exchanges R1 at 07:15 and gets R2, then exchanges R2 at 07:30 and gets R3. At 07:45 the server receives R1 again. Which tokens get revoked?

The whole family: R1, R2 and R3. R1 was already exchanged and appears again, so a copy exists, and the server can't tell which holder is real. That's why Siti's R3 is revoked too. Siti only has to log in again with her password. Whoever holds the copy can't.

Meaning: The server revokes every token in that family, but the answer to the client stays a plain 401. The details go to the server log (the record of what happens on the server), not into the answer (the response).

⚡ The order of checks on the server

Write:

function refresh(token, now) {
  const entry = store.get(token);
  if (!entry || entry.revokedAt !== null || entry.expiresAt <= now) return { status: 401 };
  if (entry.usedAt !== null) {
    revokeFamily(entry.familyId, now); // used twice: revoke the whole family
    return { status: 401, reuse: true };
  }
  entry.usedAt = now;
  return { status: 200, refreshToken: issue(entry.userId, entry.familyId, now) };
}

Meaning: store holds the data of each refresh token, and entry is the data of the token that was sent. A token that is unknown, revoked or expired gets a plain rejection. Only after that is usedAt checked: a valid token that was already exchanged is being used again, so revokeFamily revokes its whole family. If it was never exchanged, issue makes a new token with the same familyId, so that revokeFamily can find them all later.

What you will learn