Course contentsShow
← Web Development
Lesson 1611 of 2,98229. Web Authentication PatternsFree lesson

Token Rotation on Refresh

Understand refresh token rotation where each refresh returns a new refresh token, improving security against replay attacks.

Token Rotation on Refresh

What you'll learn: How rotating refresh tokens on every use prevents replay attacks and improves JWT security.

The Core Idea

Token rotation means that every time a client uses a refresh token to get a new access token, the server also issues a brand new refresh token and invalidates the old one. Instead of a single long-lived refresh token being reused repeatedly, each refresh operation produces a fresh token pair.

Think of it like a movie ticket that transforms into a new ticket stub each time you re-enter the theater. If someone steals your old stub, it's useless because you're already holding the latest one.

How It Works

When your frontend sends a refresh token to get a new access token:

  1. Server validates the refresh token
  2. Server generates a new access token
  3. Server generates a new refresh token
  4. Server invalidates the old refresh token in the database
  5. Server returns both new tokens to the client

The client must store the new refresh token, replacing the old one. If the old token is ever used again, the server knows something's wrong—either it's a replay attack or the client didn't properly update its stored token.

Why This Matters

Security against replay attacks: If an attacker steals a refresh token through network interception, they have a very narrow window to use it. Once the legitimate client refreshes and gets a new token, the stolen one becomes invalid.

Breach detection: If both the legitimate user and an attacker try to use different versions of the refresh token, the server can detect suspicious activity and revoke all tokens for that session.

Key Takeaway: Token rotation treats refresh tokens as single-use credentials, dramatically reducing the damage from token theft by ensuring each refresh operation invalidates the previous token.