Authentication: Sessions vs JWT
Authentication proves who the user is; authorization decides what they may do. The two common ways to keep a user logged in are server-side sessions and JSON Web Tokens. Interviewers expect you to compare them honestly, including how to log a user out.
What you will learn
- Separate authentication from authorization
- Explain how session cookies and JWTs work
- Design access and refresh tokens with rotation
- Know where to store tokens in the browser
Key concepts
Session
After login the server stores session data (in Redis or a database) and sends a random session id in an HttpOnly cookie. Each request looks the id up. Logout or revoke = delete the session.
JWT
A signed token with three base64url parts: header, payload (claims such as sub, exp, roles) and signature. The server verifies the signature without a lookup. It is signed, not encrypted: anyone can read the payload.
Access + refresh tokens
A short-lived access token (5 to 15 minutes) for API calls and a long-lived refresh token, stored server-side or as an HttpOnly cookie, to get new access tokens. Rotate the refresh token on every use and revoke the family if an old one is reused.
RBAC
Role-based access control: users have roles, roles have permissions. Check permissions on the server for every request, including ownership (this order belongs to this user).
Step by step
Sessions vs JWT, honestly
- Sessions: easy revocation, small cookie, needs a shared store (Redis) when you scale out
- JWT: no lookup per request, works across services, but revocation needs a deny-list or short expiry
- For a normal web app on one domain, sessions are simpler and safer
- JWTs fit service-to-service calls and APIs used by mobile apps
Where to keep tokens in the browser
localStorage is readable by any script, so one XSS bug leaks the token. Prefer HttpOnly, Secure, SameSite cookies, and add CSRF protection for state-changing requests.
Examples
Issuing and verifying JWTs
import jwt from 'jsonwebtoken'
const access = jwt.sign({ sub: user.id, roles: user.roles }, process.env.JWT_SECRET, { expiresIn: '15m', issuer: 'api.example.com' })
// verify: always pin the algorithm
const claims = jwt.verify(token, process.env.JWT_SECRET, { algorithms: ['HS256'], issuer: 'api.example.com' })Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Implement refresh-token rotation with reuse detection.
- Explain how you would force-logout a user on every device with sessions and with JWTs.
- Add an ownership check so users can only read their own orders.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Can you invalidate a JWT?
Not by itself; it stays valid until exp. Options: keep access tokens short-lived, keep a deny-list of token ids (jti) in Redis until they expire, or store a token version per user and reject older versions.
What is the "alg: none" attack?
Some old libraries accepted a token whose header said alg: none, so it needed no signature. Always pass the allowed algorithms when verifying and use maintained libraries.
What is IDOR?
Insecure Direct Object Reference: the API trusts an id from the URL (/orders/123) without checking that the record belongs to the caller. Fix it with ownership checks in every query (WHERE id = ? AND user_id = ?).
Summary
Sessions are simple and revocable; JWTs are stateless but hard to revoke, so keep them short-lived with rotating refresh tokens. Store tokens in HttpOnly cookies and check authorization, including ownership, on every request.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus