Access Tokens, Refresh Tokens, and Token Lifetime
You'll learn how different token types work together to balance security and user experience in OAuth2 systems.
The Two-Token System
In OAuth2, you typically deal with two types of tokens. Access tokens are short-lived credentials that grant access to protected resources—think of them as temporary visitor badges. Refresh tokens are long-lived credentials used solely to obtain new access tokens when the old ones expire—like a special ID card you show at the security desk to get a new visitor badge.
This separation exists for security: if an access token is compromised, it expires quickly, limiting damage. The refresh token stays secure (stored server-side or in secure storage) and is rarely transmitted.
Token Lifetime Strategy
Access tokens typically expire in minutes to hours (15-60 minutes is common). This short window minimizes risk if intercepted. Refresh tokens can last days, weeks, or even months, allowing users to stay logged in without constant re-authentication.
When an access token expires, your application uses the refresh token to request a new one from the authorization server—all without user interaction.
Token Rotation
Modern security practices recommend refresh token rotation: each time you use a refresh token to get a new access token, you also receive a new refresh token. The old one becomes invalid. This limits the window of vulnerability if a refresh token leaks.
Storage Considerations
Store access tokens in memory when possible (ephemeral). Refresh tokens require persistent storage but must be secured—never in localStorage or sessionStorage where JavaScript can access them. HttpOnly cookies or secure native storage are preferred options.
Key Takeaway: Access tokens provide short-lived access to resources, refresh tokens enable seamless renewal without re-authentication, and proper lifetime management with rotation significantly improves security posture.