Tutorial

JWT Refresh Token Rotation

Rotate refresh tokens on each use, detect reuse as a theft signal, and keep access tokens short without forcing re-login on every call.

On this page

  1. Introduction
  2. Keep a server record
  3. Rotate on each use
  4. Detect reuse
  5. Short access tokens
  6. Bind to family
  7. Test the theft
  8. Sources

Introduction

This guide covers refresh token rotation with reuse detection, a recurring concern in the auth-jwt scenario. A long-lived refresh token is a stolen-credential liability: whoever grabs it can mint access tokens for weeks. Rotating the refresh token on each use and refusing a reused one turns a stolen token into an alarm, because the legitimate client and the thief cannot both present the same refresh token without one of them triggering reuse detection.

Keep a server record

Store the refresh token id and its family in a table so you can detect reuse and invalidate a family, which is the state that makes rotation more than a refresh.

The trade-off worth naming is that store the refresh token id and its family in a table so you can detect reuse and invalidate a family, which is the state that makes rotation more than a refresh adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

CREATE TABLE refresh_token (
  id TEXT PRIMARY KEY,
  family TEXT NOT NULL,
  user_id TEXT NOT NULL,
  revoked BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMPTZ DEFAULT now()
);

Rotate on each use

When a refresh token is used, mark it revoked and issue a new one in the same family, so each refresh token is single-use and a replay is a reuse.

The trade-off worth naming is that when a refresh token is used, mark it revoked and issue a new one in the same family, so each refresh token is single-use and a replay is a reuse adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function rotate(oldId, family, userId) {
  await db.query('UPDATE refresh_token SET revoked=true WHERE id=$1', [oldId]);
  const newId = crypto.randomUUID();
  await db.query('INSERT INTO refresh_token(id,family,user_id) VALUES($1,$2,$3)', [newId, family, userId]);
  return sign({ jti: newId, sub: userId });
}

Detect reuse

If a revoked token in a family is presented again, revoke the whole family and force re-login, which is the theft signal that rotation exists to produce.

The trade-off worth naming is that if a revoked token in a family is presented again, revoke the whole family and force re-login, which is the theft signal that rotation exists to produce adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Short access tokens

Keep access tokens at five to fifteen minutes so a stolen one expires before it matters, and let the refresh token ride in an httpOnly cookie.

The trade-off worth naming is that keep access tokens at five to fifteen minutes so a stolen one expires before it matters, and let the refresh token ride in an httponly cookie adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Bind to family

Issue all refresh tokens from one login under one family id so reuse detection can revoke them as a unit, which is the granularity that matches a theft.

The trade-off worth naming is that issue all refresh tokens from one login under one family id so reuse detection can revoke them as a unit, which is the granularity that matches a theft adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

function login(userId) {
  const family = crypto.randomUUID();
  const jti = crypto.randomUUID();
  return sign({ jti, f: family, sub: userId }, REFRESH_SECRET, { expiresIn: '30d' });
}

Test the theft

Simulate a stolen refresh token used after the legitimate client rotated, and assert the family is revoked and the stolen token is refused, so rotation is real.

The trade-off worth naming is that simulate a stolen refresh token used after the legitimate client rotated, and assert the family is revoked and the stolen token is refused, so rotation is real adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: RFC 6749, JWT best practices.

FAQ

What is a token family?
A family groups all refresh tokens descended from one login; reuse detection invalidates the whole family, so a thief who triggers it loses access and the legitimate user is forced to re-authenticate.
Should the access token also rotate?
It rotates on each refresh, which is cheap because it is stateless; the point of a short access token is that even a stolen one expires in minutes.

Related Examples