Menu

Refresh token rotation in Express + TypeScript, explained (with a free boilerplate)
📰

Refresh token rotation in Express + TypeScript, explained (with a free boilerplate)

DEV Community: typescript·Ivo Baccino·about 9 hours ago
#7cACro0N
#devtotypescript#devto#dev#token#refresh
Reading 0:00
15s threshold

Most Express auth tutorials stop at "sign a JWT, put it in a header, done." That works until you need to log someone out, or until a token leaks. Then you discover a JWT can't be revoked.

In this post I'll walk through the pattern I use instead: short-lived access tokens + rotating refresh tokens with reuse detection . All the code comes from an open-source starter I published, linked at the end.

The problem with long-lived JWTs

A JWT is valid until it expires. The server doesn't keep a list of them, which is the whole point: no database lookup per request.

That also means:

You can't log a user out of other devices.

If a token is stolen, the attacker keeps access until it expires.

Making it expire in 15 minutes fixes that, but now users log in every 15 minutes.

The fix: two tokens with two jobs

Access token Refresh token

Format JWT Random opaque string

Lifetime 15 minutes 30 days

Stored on server No Yes, hashed

Sent on Every API request Only to /auth/refresh

Revocable No (but expires fast)…

Continue reading — create a free account

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