Mohamed GhoniemWeb Developer
MohamedGhoniem
  • Work
  • Services
  • Experience
  • Blog
Available
CV
Open to opportunities
WorkServicesExperienceBlogDownload CV
All blogs

© 2026 Mohamed Ghoniem. All rights reserved.

TwitterGitHubLinkedIn
JWT refresh token rotation — the right way
DesignFrontend
All blogs

JWT refresh token rotation — the right way

MG
Mohamed Ghoniem
·July 16, 2026·6 min read

Prefer to watch?

This article has a full video walkthrough on YouTube

Watch the same walkthrough on YouTube.

The problem with standard JWT authentication

JSON Web Tokens (JWT) are stateless and simple, but they pose a security challenge: if a token has a long expiration time and gets stolen, an attacker can access the system until the token expires. If it has a short expiration time, users are forced to log in repeatedly.

Refresh Token Rotation to the rescue

To solve this, we use short-lived access tokens (e.g., 15 minutes) and a long-lived refresh token stored in a secure, httpOnly cookie. Whenever the access token expires, a request is made to rotate the refresh token and generate a new access token.

javascript
// Express handler for token rotation
app.post('/refresh-token', async (req, res) => {
  const { refreshToken } = req.cookies;
  if (!refreshToken) return res.sendStatus(401);
  
  // Find token, verify, check if reused (potential theft)
  const user = await verifyRefreshToken(refreshToken);
  if (!user) return res.sendStatus(403);
  
  const newAccessToken = generateAccessToken(user);
  const newRefreshToken = generateRefreshToken(user);
  
  // Rotate and save
  await updateRefreshTokenFamily(refreshToken, newRefreshToken);
  
  res.cookie('refreshToken', newRefreshToken, { httpOnly: true, secure: true });
  res.json({ accessToken: newAccessToken });
});

Handling token reuse detection

If a refresh token is reused, we must immediately invalidate the entire family of refresh tokens associated with that user. This protects users in case an attacker steals a refresh token, because as soon as either party tries to use it again, access is revoked.

On this page6 min left
The problem with standard JWT authenticationRefresh Token Rotation to the rescueHandling token reuse detection

Share

On this page6 min left
The problem with standard JWT authenticationRefresh Token Rotation to the rescueHandling token reuse detection

Written by

Mohamed Ghoniem

I build fast, responsive web experiences, from pixel-perfect UI to the backend behind it.

Get in touchAll posts
Older postSmooth page transitions with Framer Motion and Next.js App RouterNewer postThe hidden cost of unoptimized images in Next.js
Keep reading
Building a design system from scratch with Figma tokens & Tailwind
DesignFrontendJuly 16, 2026

Building a design system from scratch with Figma tokens & Tailwind

A practical walkthrough of how I set up a token-driven design system that stays in sync between Figma and code — no plugin lock-in, no manual updates.

MG
Mohamed G.
8 min read
The hidden cost of unoptimized images in Next.js
DesignFrontendJuly 16, 2026

The hidden cost of unoptimized images in Next.js

A practical walkthrough of how I set up a token-driven design system that stays in sync between Figma and code — no plugin lock-in, no manual updates.

MG
Mohamed G.
5 min read
Smooth page transitions with Framer Motion and Next.js App Router
DesignFrontendJuly 16, 2026

Smooth page transitions with Framer Motion and Next.js App Router

A practical walkthrough of how I set up a token-driven design system that stays in sync between Figma and code — no plugin lock-in, no manual updates.

MG
Mohamed G.
7 min read