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

Access Token vs Refresh Token Lifecycle

Understand the different expiration times and purposes of access tokens versus refresh tokens in authentication flows.

Access Token vs Refresh Token Lifecycle

What you'll learn: You'll understand why access tokens and refresh tokens have dramatically different lifespans and how they work together in secure authentication systems.

The Two-Token System

Think of access tokens and refresh tokens like a hotel key card and your driver's license. The key card (access token) expires daily for security, but your license (refresh token) lasts years and can get you a new key card when needed.

Access Token Lifecycle

Access tokens are short-lived, typically expiring in 15 minutes to 1 hour. They're designed to be lightweight and frequently transmitted with every API request. Their brief lifespan limits the damage if one is stolen — the attacker only has a small window to misuse it.

// Access token with short expiration
{
  "access_token": "eyJhbG...",
  "expires_in": 900  // 15 minutes
}

If an access token is compromised, it becomes useless after 15 minutes without any action needed from you or the server.

Refresh Token Lifecycle

Refresh tokens are long-lived, lasting days, weeks, or even months. They're stored more securely (often in HttpOnly cookies) and used only occasionally — specifically when the access token expires. Because they're rarely transmitted, they face less exposure to network attacks.

// Refresh token with long expiration
{
  "refresh_token": "dGhpc2lz...",
  "expires_in": 2592000  // 30 days
}

Why Different Lifespans Matter

Short access tokens mean frequent "check-ins" with minimal risk. Long refresh tokens mean users don't have to log in every 15 minutes, but you can still revoke them server-side if needed (logout, suspicious activity). This creates a balance between security and user experience.

Key Takeaway: Access tokens expire quickly (minutes) for security and are used constantly, while refresh tokens last much longer (days/weeks) and are used sparingly to obtain fresh access tokens without requiring re-authentication.