Course contentsShow
← Software Engineering
Lesson 1013 of 1,93331. Authentication and AuthorizationFree lesson

Token Refresh and Rotation

Using refresh tokens to obtain new access tokens without re-authentication and rotation strategies.

Token Refresh and Rotation

What you'll learn: How to keep users authenticated longer without compromising security by using refresh tokens and rotation strategies.

The Problem

Access tokens (like JWTs) should have short lifespans for security—if stolen, they're only dangerous for minutes. But forcing users to re-authenticate every few minutes creates terrible UX. How do you balance security with convenience?

The Refresh Token Solution

A refresh token is a long-lived credential issued alongside your short-lived access token. When the access token expires, the client sends the refresh token to a dedicated endpoint to receive a fresh access token—without requiring the user to log in again.

Think of it like a gym membership card (refresh token) that lets you get daily entry passes (access tokens). The daily pass expires quickly, but your membership lets you get a new one without re-registering.

How It Works

  1. User authenticates successfully
  2. Server issues both an access token (expires in 15 minutes) and a refresh token (expires in 7 days)
  3. Client uses access token for API requests
  4. When access token expires, client calls /refresh endpoint with refresh token
  5. Server validates refresh token and issues a new access token

Token Rotation

Token rotation means issuing a new refresh token every time one is used—the old refresh token becomes invalid. This limits the damage if a refresh token is stolen: it only works once, and the legitimate user's next refresh will fail, alerting you to compromise.

Some systems rotate on every refresh; others rotate only after certain conditions (time passed, suspicious activity).

Storage Matters

Refresh tokens require careful storage—they're more powerful than access tokens. Store them securely (HttpOnly cookies for web, secure storage APIs for mobile), never in localStorage.

Key Takeaway: Refresh tokens let you maintain long-term sessions with short-lived access tokens, and rotation strategies add an extra security layer by limiting the window of vulnerability when tokens are compromised.