Загрузка...

🎯 JWT Token Expiry in Production — How would you redesign it? #jwt #techinterview #interviewprep

🎯 Interview Question: “Your JWT token is getting expired frequently in production. Users need to login repeatedly. How will you redesign token management?”

This question checks:
✅ Authentication flow
✅ JWT lifecycle management
✅ Security vs User Experience tradeoff
✅ Refresh tokens
✅ Token revocation
✅ Microservices understanding

Step 1: Identify the problem
Suppose Access Token expires every 15 mins:
❌ Users login repeatedly
❌ Poor user experience
❌ More load on auth server
Need a better design.

Step 2: Use Access Token + Refresh Token
Industry standard approach:
🔹 Access Token (Short-lived)
• Expiry → 10–15 mins
• Used in API calls
• Short expiry improves security
Example:
Authorization: Bearer access_token
🔹 Refresh Token (Long-lived)
• Expiry → 7/30/90 days
• Generates new access tokens
• Avoids repeated login
Flow:
Login

Server returns:
Access Token → 15 mins
Refresh Token → 30 days

Access token expires

Client sends Refresh Token

Server validates it

Issues new Access Token

User continues without login

Step 3: Secure storage
❌ Store in LocalStorage → Risk of XSS attack
✅ Better:
HttpOnly Secure Cookie
Benefits:
✓ JavaScript cannot access it
✓ More secure

Step 4: Refresh Token Rotation (Best Practice)
Whenever refresh token is used:
Old Refresh Token → Invalidate ❌
Generate:
New Refresh Token + New Access Token ✅
Benefit:
If old refresh token is stolen → Useless after rotation

Step 5: Token Revocation
Problem:
JWT is stateless.
After logout, token may still work until expiry.
Solution:
Maintain revoked tokens in:
• Redis
• Database
During validation:
Check → Is token revoked?
If yes → Reject request

Step 6: Sliding Session
Active users:
Keep extending session
Example:
Expire in 30 mins
→ User active
→ Extend another 30 mins
Inactive users expire naturally.

Step 7: Microservices Design
Client

Auth Service

Issues JWT

Microservices validate JWT

Refresh handled only by Auth Service → More scalable

🚀 Answer:
"I’d use short-lived access tokens with long-lived refresh tokens, secure storage, token rotation, revocation using Redis, and optional sliding sessions. This improves security, scalability, and user experience."

This shows production-level thinking beyond JWT basics.

#jwt #techinterview #interviewprep #learnwithme

Видео 🎯 JWT Token Expiry in Production — How would you redesign it? #jwt #techinterview #interviewprep канала Aparna Srivastava
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять