š Session Token vs Refresh Token ā The Simplest Explanation!
April 15, 2025

Modern web apps run on tokens. But which one does what? Letās break down Session Tokens and Refresh Tokens so clearly that youāll never forget, and confidently implement them in any project. LetāsĀ go from concept to codeĀ and build your solid understanding with: š§Ā What to Build (Token System Essentials) šĀ Functions Youāll Need šĀ Algorithms/Flows š ļøĀ Tools & Libraries [ā¦]
Modern web apps run on tokens. But which one does what? Let’s break down Session Tokens and Refresh Tokens so clearly that youāll never forget, and confidently implement them in any project.
LetāsĀ go from concept to codeĀ and build your solid understanding with:
- š§Ā What to Build (Token System Essentials)
- šĀ Functions Youāll Need
- šĀ Algorithms/Flows
- š ļøĀ Tools & Libraries (with alternatives)
- šĀ Trendy/Best Practices
- š¢Ā What Big Tech Uses
š§ 1. WHAT TO BUILD ā Token Auth System in Any App
Any software (web, mobile, API-based) with token authentication will haveĀ 3 main parts:
Step | Action |
---|---|
š 1 | Login (generate access + refresh tokens) |
š 2 | Refresh token (get new access token) |
š 3 | Logout (invalidate refresh token) |
š 2. REQUIRED FUNCTIONS (in pseudo + JS-style)
You typically need to write 5Ā core functions:
// 1. Login
function login(email, password) {
// validate user
// generate accessToken + refreshToken
// store refreshToken securely (DB or cookie)
}
// 2. Generate Access Token
function generateAccessToken(user) {
// return jwt.sign(user, secret, { expiresIn: '15m' })
}
// 3. Generate Refresh Token
function generateRefreshToken(user) {
// return jwt.sign(user, refreshSecret, { expiresIn: '7d' })
}
// 4. Refresh Token Endpoint
function refresh(req) {
// validate refreshToken
// if valid => issue new accessToken
}
// 5. Logout
function logout(req) {
// remove/invalidate refreshToken
}
Add:
- ā
Ā
middleware
Ā toĀ check access tokenĀ on every API call - š TokenĀ rotationĀ strategy
š 3. ALGORITHM FLOW (Pseudocode)
A. Login Flow
User submits email + password
ā
If valid:
ā generate access token (15 mins)
ā generate refresh token (7 days)
ā send access in body, refresh in HTTP-only cookie
B. API Request Flow
Frontend sends access token in headers
ā
Backend verifies token
ā
If valid ā grant access
If expired ā ask frontend to refresh token
C. Token Refresh Flow
Frontend sends refresh token (cookie)
ā
Backend verifies it
ā
If valid ā issue new access token (maybe refresh token too)
ā
Frontend replaces old token and continues
D. Logout Flow
User clicks logout
ā
Frontend deletes tokens (cookie/localStorage)
ā
Backend blacklists or deletes refresh token from DB
š ļø 4. TOOLS TO USE
š Token Generator
jsonwebtoken
Ā (Node.js)pyjwt
Ā (Python)nimbus-jose-jwt
Ā (Java)
š¦ Session Store (Optional)
- Redis (store refresh token or blacklist tokens)
- In-memory (for demo)
- Database (Mongo, Postgres)
šŖ Cookie Management
cookie-parser
Ā (Node)HttpOnly
Ā +ĀSameSite=Strict
Ā for refresh tokens
š Auth Libs (if you want ready-made)
NextAuth.js
Ā (Next.js)Passport.js
Ā (Node)Firebase Auth
Ā (Google, prebuilt solution)Supabase Auth
Ā (Backendless)
ā” 5. TRENDY BEST PRACTICES
ā UseĀ short-lived access tokensĀ (15m to 1h)
ā UseĀ refresh tokensĀ with rotation (and maybe detection of reuse)
ā Store refresh token inĀ HTTP-only secure cookies, never in localStorage
ā Add aĀ logout-all-devicesĀ orĀ token revokeĀ option
ā UseĀ middleware/auth guardĀ in APIs/routes
⨠Extra: Use a queue (e.g., Redis) to store a blacklist of used refresh tokens (detect hijacking)
š¢ 6. BIG TECH STRATEGY
Company | Auth System | Notes |
---|---|---|
Session cookie-based (internal), tokens for APIs | Uses long-lived refresh system | |
OAuth2 + OpenID + JWT | Access & Refresh tokens, stored securely | |
Discord | Access token + refresh token flow | Like OAuth2 spec |
Spotify | Strict refresh token rotation, OAuth2 | Modern best practices |
Netflix | Short-lived access token, secure refresh handling | High emphasis on device-level auth |
š¬ Even big companiesĀ don’t keep users logged inĀ forever. TheyĀ refresh tokens in the backgroundĀ to make UX smooth.
ā What You Should Write (Almost Any Software Needs):
Backend
- Login route
- Token generation utilities
- Token refresh route
- Logout route
- Auth middleware
- Optional: Token storage in DB or Redis
Frontend
- Store access token (memory/localStorage)
- Auto-refresh tokens on expiration
- Logout flow
- Attach token to API headers
š§ Side-by-Side Snapshot
Feature | Session Token (Access) | Refresh Token |
---|---|---|
Purpose | Access APIs | Get new access tokens |
Lifespan | Short (15mā1h) | Long (daysāweeks) |
Sent with requests | ā Yes | ā No |
Risk if stolen | High (frequently exposed) | Low (stored securely) |
Storage | Memory/localStorage/cookie | HTTP-only cookie (preferred) |
Rotation | ā Optional | ā Recommended |
šÆ Quick Summary
Use access tokens for immediate API calls.
Use refresh tokens to silently renew access without asking the user to log in again.
Store refresh tokens securely. Rotate them. Invalidate them on logout.
Access token is your key to the house. Refresh token is your ability to get a new key if you lose the old one.