HeoLab
ToolsBlogAboutContact
HeoLab

Free developer tools with AI enhancement. Built for developers who ship.

Tools

  • JSON Formatter
  • JWT Decoder
  • Base64 Encoder
  • Timestamp Converter
  • Regex Tester
  • All Tools →

Resources

  • Blog
  • What is JSON?
  • JWT Deep Dive
  • Base64 Explained

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 HeoLab. All rights reserved.

Tools work in your browser. Zero data retention.

HomeBlogThe JWT Refresh Token Pattern Explained
Table of Contents▾
  • Table of Contents
  • The Problem with Long-lived JWTs
  • The Refresh Token Pattern
  • Token Rotation
  • Storing Refresh Tokens
  • Revoking Tokens
api#jwt#refresh-tokens#authentication

The JWT Refresh Token Pattern Explained

How to implement secure, long-lived sessions with short-lived access tokens and refresh tokens — the pattern used by Google, GitHub, and Stripe.

Trong Ngo
February 22, 2026
2 min read

Table of Contents

  • The Problem with Long-lived JWTs
  • The Refresh Token Pattern
  • Token Rotation
  • Storing Refresh Tokens
  • Revoking Tokens

The Problem with Long-lived JWTs

If you set your JWT expiry to 7 days and that token is stolen, the attacker has 7 days of access. You can't revoke it without a deny-list (which requires a DB lookup on every request).

The Refresh Token Pattern

Use two tokens:

TokenLifespanStoragePurpose
Access token15 minutesMemory / cookieAPI authorization
Refresh token30 dayshttpOnly cookie + DBGet a new access token

When the access token expires, the client silently exchanges the refresh token for a new one — the user never sees a login prompt.

Token Rotation

Every time a refresh token is used, issue a new refresh token and invalidate the old one. If you detect reuse of an old token, it means it was stolen — immediately revoke the entire token family.

Storing Refresh Tokens

CREATE TABLE refresh_tokens (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES users(id) ON DELETE CASCADE,
  token_hash TEXT UNIQUE NOT NULL,
  family_id UUID NOT NULL,
  expires_at TIMESTAMPTZ NOT NULL,
  revoked BOOLEAN DEFAULT false
);

Revoking Tokens

To log out all devices: delete all refresh tokens for the user from the DB. Every existing session will fail at the next refresh cycle.

Try These Tools

JWT Decoder & Inspector

Decode and inspect JSON Web Tokens. View header, payload, and verify structure instantly.

Related Articles

Base64 is Not Encryption: Security Misconceptions Explained

1 min read

Implementing JWT Authentication in Next.js

2 min read

JWT vs Session Tokens: When to Use Each

2 min read

Back to Blog

Table of Contents

  • Table of Contents
  • The Problem with Long-lived JWTs
  • The Refresh Token Pattern
  • Token Rotation
  • Storing Refresh Tokens
  • Revoking Tokens

Related Articles

Base64 is Not Encryption: Security Misconceptions Explained

1 min read

Implementing JWT Authentication in Next.js

2 min read

JWT vs Session Tokens: When to Use Each

2 min read