Menu

1 / 3

Refresh-token rotation with reuse detection in NestJS

DEV Community: nestjs·Salnor Tech·2 days ago
#YEzAaBGC
#devtonestjs#devto#dev#token#refresh
Long article (1006 words) — no AI summary yet.
Reading 0:00
15s threshold

Salnor Tech

Posted on Nov 28

Edited on Nov 28

AI-assisted

A refresh token is the most valuable string your API hands out. It lives for weeks, it mints new access tokens, and if someone copies it from a leaked log, a compromised laptop or a careless proxy, they can quietly stay logged in as your user. Most of the time, nobody notices.

Rotation with reuse detection turns that silent theft into something you can see, and stop.

The idea in three rules

Every refresh returns a new refresh token and marks the old one as used.

All tokens issued from one login share a family ID .

If a token that was already used (or revoked) comes back, someone has a copy. Revoke the whole family and record it.

The real user and the attacker now hold tokens from the same family. Whoever refreshes second triggers the alarm, and both are logged out. The real user signs in again; the attacker has nothing.

What it looks like

async refresh ( rawToken : string , context : RequestContext ) { const token = await this .…

Continue reading — create a free account

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