Refresh Token Storage Strategy
You'll learn: How to store refresh tokens separately with stricter security measures than access tokens to minimize attack surface.
Why Separate Storage Matters
Refresh tokens are like master keys that can create new access tokens, often for days or weeks. Access tokens are daily passes that expire quickly. If someone steals your master key, they can keep making new daily passes indefinitely. Because refresh tokens have this power and longer lifespan, they need maximum protection—even if your access token strategy is less strict.
The Core Strategy
Don't treat them the same. Even if you store access tokens in localStorage or memory (risky but sometimes done), your refresh token should get more protection:
- Access token: May live in memory or
sessionStorage(expires when tab closes) - Refresh token: Should be in an HttpOnly cookie (JavaScript can't read it) or never sent to the frontend at all
This separation means that even if XSS exposes your access token, attackers can't steal the refresh token to maintain long-term access.
Common Patterns
- HttpOnly + Secure Cookie: Server sends refresh token as HttpOnly, Secure, SameSite cookie. Frontend never sees it—only the browser includes it automatically when calling
/refresh. - Backend-Only Storage: Refresh token stays on the server (in Redis, database). Frontend never receives it. Server links it to a session ID.
- Split Strategy: Short-lived access token in memory, refresh token in HttpOnly cookie.
Key Takeaway: Always store refresh tokens with higher security than access tokens—preferably in HttpOnly cookies or server-side—to limit exposure and prevent attackers from maintaining persistent unauthorized access.