Frontend
- React (Vite + TypeScript)
- Axios (interceptors)
- Chakra UI
Backend
- Node.js
- Express
- MongoDB
Security
- JWT (Access + Refresh Token split)
- bcrypt password hashing
- Rate limiting
- HttpOnly cookies
- Validate user existence
- Hash password using bcrypt
- Store hashed password in MongoDB
-
Compare password using bcrypt
-
Generate:
- Access Token (short-lived)
- Refresh Token (long-lived)
- Middleware verifies access token
- Attaches
req.userId - Returns 401 if invalid or expired
- Applied on login route
- Prevents brute-force attacks
- Returns 429 when exceeded
- Short expiry (e.g., 30s)
- Sent via Authorization header
- Stored in localStorage
- Longer expiry (e.g., 5m)
- Stored in HttpOnly cookie
- Not accessible via JavaScript
- Automatically attaches access token to headers
-
If 401:
- Calls
/auth/refresh - Gets new access token
- Retries original request
- Calls
-
Prevents infinite refresh loops
Backend:
httpOnly: truesameSite: 'lax'secure: false(dev)withCredentials: trueenabled
Frontend:
- Axios configured with
withCredentials: true
On page refresh:
- Access token checked
/profileverified- Silent refresh triggered if expired
- User remains logged in
Frontend:
- Calls
/auth/logout - Removes access token
Backend:
- Clears refresh cookie
✔ Secure token lifecycle management ✔ Silent access token renewal ✔ HttpOnly refresh token security ✔ Brute-force protection ✔ Clean logout handling ✔ Stable session behavior ✔ No infinite refresh loops
This is:
- Beyond tutorial-level JWT auth
- Production-ready for small SaaS
- Interview-ready authentication architecture
- Mid-level backend engineering implementation
- Refresh token rotation
- Store refresh tokens in database
- Logout all devices
- Session/device tracking
- Role-based access control (RBAC)
- CSRF protection
- Monitoring & suspicious login detection
Part 1: Completed ✅ Part 2: Completed ✅
System is stable, secure, and production-aligned.