Menu

1 / 2

Refresh Token Rotation in NestJS and Next.js: Reuse Detection, Token Families, and the Parallel-Refresh Race — Field Notes

DEV Community: nestjs·Ahmed Mahmoud·about 6 hours ago
#ruUGFxoT
#devtonestjs#devto#dev#refresh#token
Long article (2940 words) — no AI summary yet.
Reading 0:00
15s threshold

Headline: Refresh token rotation means the server issues a new refresh token on every refresh and invalidates the old one; reuse detection means that when an already-rotated refresh token comes back, the server revokes every token from that login. In a NestJS API behind a Next.js App Router frontend, I store only SHA-256 hashes of opaque refresh tokens in Postgres, rotate them with one compare-and-set UPDATE , and allow a 10-second grace window so parallel refreshes from the same browser are not mistaken for theft.

The session design I started from is a common one: a short-lived JWT access token and a 30-day refresh token, both in httpOnly cookies, and a refresh endpoint that signs a new access token and leaves the refresh token untouched. It works until the refresh token leaks. Whoever holds a copy has a 30-day session, and the server cannot tell the two holders apart.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.