JWT Authentication¶
Interview-Oriented Handbook for SDE2 / Senior Backend Engineers
Table of Contents¶
- Authentication Fundamentals
- Session-Based Authentication
- Why JWT?
- JWT Architecture
- JWT Claims
Note: This is Part 1 of the handbook. The remaining sections (login flow, middleware, refresh tokens, security, production architecture, interview questions, etc.) continue in Part 2 and Part 3.
1. Authentication Fundamentals¶
What is Authentication?¶
Authentication is the process of verifying who a user is.
Whenever a client sends a request to a backend service, the server must determine whether the caller is a legitimate user or an anonymous client.
For example:
How does the server know whether the request is actually coming from John?
Authentication answers exactly this question.
"Can I trust the identity of the person making this request?"
Authentication vs Authorization¶
These two terms are frequently confused during interviews.
Authentication¶
Authentication verifies the identity of a user.
Example:
The server validates the user's credentials and confirms that the user is John.
Authorization¶
Authorization determines what the authenticated user is allowed to do.
Example:
John may be authenticated successfully but still not have permission.
Example¶
Authentication always happens before authorization.
Stateless vs Stateful Authentication¶
One of the most common interview questions.
Stateful Authentication¶
The server stores authentication information.
Example:
Every request requires the server to remember the user.
Stateless Authentication¶
The server does not store user sessions.
Instead, every request contains enough information to identify the user.
No session lookup is required.
This is the fundamental idea behind JWT.
Interviewer's Perspective¶
You should be able to answer:
- What is authentication?
- What is authorization?
- Difference between stateful and stateless authentication?
- Why is JWT called stateless?
2. Session-Based Authentication¶
Before understanding JWT, it's important to understand the traditional session-based approach.
Interviewers often ask:
Why was JWT introduced?
The answer begins with sessions.
How Session Authentication Works¶
Step 1¶
User enters credentials.
Step 2¶
Client sends request.
Step 3¶
Server validates credentials.
Usually by comparing the password hash stored in the database.
Step 4¶
Server creates a session.
Example:
This session is stored in a database or Redis.
Step 5¶
The server returns only the Session ID.
The browser stores this cookie.
Step 6¶
Every future request automatically includes the cookie.
Step 7¶
The server looks up the session.
Session Authentication Flow¶
Login
│
▼
Username + Password
│
▼
Server verifies credentials
│
▼
Create Session
│
▼
Store Session
(Database / Redis)
│
▼
Return Session Cookie
│
▼
Every Request
│
▼
Cookie Sent
│
▼
Lookup Session
│
▼
User Authenticated
Advantages of Sessions¶
Easy Logout¶
The server simply deletes the session.
Immediately logged out.
Immediate Revocation¶
If an administrator disables an account,
simply delete the session.
No future requests succeed.
Simple Implementation¶
Most web frameworks provide built-in session management.
Problems with Sessions¶
This is where interviewers usually expect some discussion.
1. Server Memory¶
The server must store every active session.
As users grow, storage requirements also grow.
2. Horizontal Scaling¶
Imagine two servers.
User logs into Server A.
The session exists only there.
Next request reaches Server B.
User appears logged out.
Solution¶
Use a centralized session store.
Usually Redis.
Now every request requires Redis.
3. Additional Network Call¶
Every authenticated request becomes:
Even a lightweight API now performs an extra lookup.
4. Mobile Applications¶
Cookies work naturally with browsers.
For mobile applications and APIs, managing session cookies is less convenient than token-based authentication.
Interviewer's Perspective¶
If asked:
Why don't modern APIs always use sessions?
A strong answer includes:
- Server-side storage
- Horizontal scaling challenges
- Additional session lookup
- Better suitability of tokens for APIs and mobile clients
3. Why JWT?¶
JWT (JSON Web Token) solves many of the problems associated with session-based authentication.
Instead of storing session information on the server,
the server sends a signed token to the client.
Every request contains that token.
The server verifies the signature.
No session lookup is required.
JWT Authentication Flow¶
Login
│
▼
Verify Credentials
│
▼
Generate JWT
│
▼
Return JWT
│
▼
Client Stores JWT
│
▼
Every Request
Authorization:
Bearer <JWT>
│
▼
Verify Signature
│
▼
Authenticated
Notice an important difference.
The server never stores the access token.
This is why JWT is considered stateless.
Benefits of JWT¶
Better Horizontal Scaling¶
Since authentication information travels with the request,
any server can validate the token.
No shared session store is required for access token validation.
Better for APIs¶
REST APIs are naturally stateless.
JWT aligns well with REST principles because every request is self-contained.
Better for Microservices¶
Every service can verify the token independently.
No centralized session lookup.
Is JWT Always Better?¶
No.
This is a common misconception.
For many traditional web applications,
sessions remain a perfectly valid choice.
JWT shines when:
- Building REST APIs
- Mobile applications
- Microservices
- Distributed systems
Interviewer's Perspective¶
If someone asks:
Why was JWT introduced?
A good answer is not:
"JWT is more secure."
Instead explain:
- Stateless authentication
- Better scalability
- Easier horizontal scaling
- Works well for APIs
- Suitable for distributed architectures
4. JWT Architecture¶
A JWT consists of three parts.
Example:
Each section is Base64URL encoded and separated by a period (.).
Part 1 - Header¶
The header contains metadata about the token.
Example:
alg¶
Specifies the signing algorithm.
Example:
We'll discuss these algorithms in Part 3.
typ¶
Usually:
This indicates the token type.
Part 2 - Payload¶
The payload contains claims.
Claims are pieces of information about the user.
Example:
The payload is not encrypted.
Anyone possessing the token can decode it.
Therefore,
never store:
- Passwords
- Credit card numbers
- OTPs
- API Keys
- Secrets
Part 3 - Signature¶
The signature proves that the token has not been modified.
Conceptually:
When a request arrives,
the server performs the same signing operation again.
If the generated signature matches the incoming signature,
the token is valid.
Otherwise,
the request is rejected.
Why Can't Someone Change the Payload?¶
Suppose the payload is:
An attacker changes it to:
The payload has changed.
However,
the signature still corresponds to the old payload.
During verification:
The server rejects the request.
This is the primary purpose of the signature.
Is JWT Encrypted?¶
No.
This is one of the most frequently asked interview questions.
JWT is:
It is not encrypted.
Anyone can decode:
- Header
- Payload
Only the signature prevents tampering.
Think of it this way:
JWT uses encoding for transport and signing for integrity, not encryption for secrecy.
Interviewer's Perspective¶
Know the difference between:
- Encoding
- Encryption
- Signing
Many candidates incorrectly say JWT is encrypted.
It is not.
5. JWT Claims¶
Claims are pieces of information stored inside the payload.
They describe the authenticated user or the token itself.
Example:
Claims fall into three categories.
- Registered Claims
- Public Claims
- Private Claims
Registered Claims¶
These are standardized claim names defined by the JWT specification.
They are optional but widely used.
sub (Subject)¶
Represents the identity of the user.
Example:
Usually this is the user's unique identifier.
iss (Issuer)¶
Identifies who generated the token.
Example:
Useful when multiple authentication providers exist.
aud (Audience)¶
Indicates which application or service should accept the token.
Example:
This prevents a token intended for one service from being accepted by another.
exp (Expiration Time)¶
Defines when the token expires.
Example:
Once expired,
the server should reject the token.
iat (Issued At)¶
Stores when the token was created.
Useful for debugging and token age calculations.
nbf (Not Before)¶
Defines when the token becomes valid.
Before this time,
the token should not be accepted.
jti (JWT ID)¶
A unique identifier for the token.
Example:
Often used for:
- Token revocation
- Blacklisting
- Refresh token rotation
Custom Claims¶
Applications often add their own claims.
Example:
Keep custom claims minimal.
A larger token increases request size because the JWT is sent with every authenticated request.
What Should Be Stored?¶
Good examples:
- User ID
- Role
- Permissions (if small)
- Tenant ID
- Organization ID
What Should NOT Be Stored?¶
Never store:
- Passwords
- Refresh Tokens
- API Keys
- OTPs
- Credit Card Numbers
- Personal secrets
Remember:
Anyone can decode the payload.
Interviewer's Perspective¶
A very common interview question is:
What information would you put inside a JWT?
A strong answer is:
"Only information required by downstream services to identify the user and make authorization decisions. Sensitive or confidential data should never be stored because the payload is only encoded, not encrypted."
6. Complete Login Flow¶
Now that we understand what JWT is, let's see how authentication is implemented in a real backend application.
This is one of the most common interview discussions because it tests whether you've actually built an authentication system.
High-Level Flow¶
Login Request
│
▼
Validate Email & Password
│
▼
Generate Access Token
│
▼
Generate Refresh Token
│
▼
Store Refresh Token (Database)
│
▼
Return Access + Refresh Token to Client
Let's understand each step.
Step 1: User Sends Credentials¶
The client sends a login request.
Step 2: Validate User¶
The server searches for the user.
If the user doesn't exist:
Step 3: Verify Password¶
Passwords should never be stored in plain text.
Instead, during registration:
Database stores:
During login:
Notice something important.
The server never decrypts passwords.
It only compares hashes.
Interviewer's Perspective¶
Question
Why don't we encrypt passwords instead of hashing them?
Expected Answer:
Encryption is reversible.
Hashing is one-way.
The server never needs to know the original password.
Step 4: Generate Access Token¶
After successful authentication:
The server signs the payload.
Result:
This becomes the Access Token.
Step 5: Generate Refresh Token¶
A refresh token is also generated.
Unlike access tokens,
refresh tokens are usually long random strings.
Example:
or another JWT depending on the implementation.
Step 6: Store Refresh Token¶
Unlike access tokens,
refresh tokens are usually stored.
Many production systems store a hash of the refresh token instead of the raw token.
This minimizes the impact if the database is compromised.
Step 7: Return Tokens¶
These are sent back to the client.
Complete Login Flow¶
Client
│
POST /login
│
▼
Authentication Service
│
Find User
│
Verify Password
│
Generate Access Token
│
Generate Refresh Token
│
Store Refresh Token
│
Return Tokens
│
▼
Client Stores Tokens
7. Where Should Tokens Be Stored?¶
This is one of the most frequently asked interview questions.
There is no universal answer.
It depends on the client.
Option 1: Local Storage¶
Advantages
- Easy to implement
- Survives page refresh
Disadvantages
- Vulnerable to XSS attacks
- JavaScript can read the token
Generally not recommended for sensitive applications.
Option 2: Session Storage¶
Advantages
- Cleared when the browser tab closes
Disadvantages
- Still accessible via JavaScript
- Vulnerable to XSS
Option 3: HttpOnly Cookies (Recommended)¶
Advantages
- JavaScript cannot read the cookie
- Better protection against token theft via XSS
Disadvantages
- Cookies are automatically sent by the browser
- Requires CSRF protection
Local Storage vs HttpOnly Cookies¶
| Feature | Local Storage | HttpOnly Cookie |
|---|---|---|
| Accessible via JavaScript | ✅ Yes | ❌ No |
| Vulnerable to XSS token theft | High | Low |
| Automatically sent by browser | ❌ No | ✅ Yes |
| Needs CSRF protection | ❌ No | ✅ Yes |
Interviewer's Perspective¶
If asked:
Where would you store JWT?
A good answer:
For browser-based applications, I prefer HttpOnly Secure cookies because JavaScript cannot access them. If cookies are used, I also enable SameSite and CSRF protection. For mobile apps, secure platform-specific storage such as the iOS Keychain or Android Keystore is typically used.
8. Authentication Middleware¶
Every protected API should pass through authentication middleware.
Instead of checking authentication in every controller,
we centralize it.
Middleware Flow¶
Incoming Request
│
Authorization Header
│
Extract Token
│
Verify Signature
│
Check Expiry
│
Extract Claims
│
Attach User
│
Next Middleware
│
Controller
Authorization Header¶
Clients send:
Example:
Token Extraction¶
Middleware extracts:
If missing:
Verify Signature¶
The server recalculates the signature.
If the signatures don't match,
reject the request.
Check Expiration¶
If yes:
Extract Claims¶
After verification:
Now the application knows:
- User ID
- Role
- Tenant
- Organization
Attach User to Request¶
Many frameworks attach the authenticated user.
Example:
Controllers no longer need to parse JWTs.
Controller¶
Simple and clean.
Interviewer's Perspective¶
Don't say:
Every controller validates JWT.
Instead explain:
Authentication is handled once in middleware.
Controllers only use the authenticated user.
9. Access Token vs Refresh Token¶
One of the most important interview topics.
Why Not Make Access Tokens Valid Forever?¶
Suppose:
If stolen,
an attacker has access for an entire year.
That's dangerous.
Short-Lived Access Tokens¶
Instead:
Even if stolen,
damage is limited.
But Users Shouldn't Login Every 15 Minutes¶
That's where refresh tokens help.
Authentication Flow¶
Login
↓
Access Token
+
Refresh Token
↓
API Calls
↓
Access Token Expires
↓
Refresh Endpoint
↓
New Access Token
↓
Continue
Refresh Endpoint¶
Example:
Client sends:
The server verifies it.
If valid,
a new access token is issued.
Why Store Refresh Tokens?¶
Access Tokens are stateless.
Refresh Tokens usually are not.
Database:
Now logout becomes possible.
Interviewer's Perspective¶
If asked:
Why don't we use only refresh tokens?
Expected Answer:
Refresh tokens are long-lived and should be used sparingly.
Every API request should use a short-lived access token.
10. Refresh Token Rotation¶
Many companies implement refresh token rotation.
Instead of reusing the same refresh token forever,
every refresh generates a new one.
Without Rotation¶
If stolen,
an attacker can continue using it.
With Rotation¶
Only the latest token remains valid.
Reuse Detection¶
Suppose:
An attacker steals Refresh Token A.
Later,
the attacker tries using Token A.
The server can immediately revoke the entire refresh token family and require the user to log in again.
This is called refresh token reuse detection.
Interviewer's Perspective¶
Senior interviewers love this topic because it shows production experience.
Mentioning refresh token rotation immediately differentiates you from candidates who have only implemented basic JWT authentication.
11. Logout¶
A classic interview question.
JWT is stateless.
So how does logout work?
Problem¶
Access Tokens are self-contained.
The server doesn't store them.
Therefore:
Option 1: Wait for Expiry¶
If access token expires in:
Logout simply deletes it from the client.
Worst case:
The token remains usable until expiry.
This is the simplest and most common strategy.
Option 2: Blacklisting¶
Store revoked token IDs.
Every request:
Disadvantage:
JWT is no longer completely stateless because every request now requires a blacklist lookup.
Option 3: Revoke Refresh Token (Most Common)¶
This is how most production systems implement logout.
Interviewer's Perspective¶
If asked:
Can JWT be immediately revoked?
Answer:
Not by itself.
Common approaches include:
- Short-lived access tokens
- Refresh token revocation
- Blacklists for special cases
12. Common Authentication APIs¶
Typical backend APIs:
/register¶
Creates a new user.
Stores hashed password.
/login¶
Verifies credentials.
Returns access token and refresh token.
/refresh¶
Validates refresh token.
Issues a new access token.
Often rotates the refresh token as well.
/logout¶
Deletes or revokes refresh token.
/me¶
Returns authenticated user information.
Authentication middleware has already populated the user context.
13. JWT Security¶
Authentication is not only about generating tokens.
A secure authentication system should also defend against common attack vectors.
Senior interviewers often ask scenario-based questions instead of asking how JWT works.
Attack 1: Token Tampering¶
Suppose an attacker has the following payload:
The attacker changes it to:
Will it work?
No.
Why?
Because changing the payload changes the signature.
The server recalculates the signature during verification.
The request is rejected.
Attack 2: Reading JWT Contents¶
Many developers believe JWT is encrypted.
It is not.
Anyone can decode:
- Header
- Payload
For example:
This information is visible to anyone possessing the token.
Therefore:
Never store
- Passwords
- API Keys
- OTPs
- Credit Card Numbers
- Secrets
inside a JWT.
JWT guarantees integrity, not confidentiality.
Attack 3: Token Theft¶
Suppose an attacker steals a user's access token.
Can they use it?
Yes.
Until it expires.
That's why access tokens should have a short lifetime.
Example:
The shorter the expiry,
the smaller the impact of token theft.
Attack 4: XSS (Cross Site Scripting)¶
Suppose the application stores JWT in Local Storage.
Malicious JavaScript executes:
The attacker now owns the JWT.
Protection¶
Use:
JavaScript cannot read HttpOnly cookies.
Even if XSS exists,
stealing the token becomes significantly harder.
Attack 5: CSRF (Cross Site Request Forgery)¶
Cookies are automatically sent by browsers.
An attacker tricks the user into visiting:
The browser automatically includes authentication cookies.
The victim unknowingly performs authenticated actions.
Protection¶
Use:
- SameSite=Lax or Strict
- CSRF Tokens
- Origin validation
Remember:
There is no perfect storage mechanism.
Every option has tradeoffs.
Attack 6: Secret Key Leakage¶
Suppose your JWT secret leaks.
An attacker can now generate valid JWTs.
This is one of the most severe security incidents.
Protection¶
- Store secrets in Secret Managers
- Never hardcode secrets
- Rotate secrets regularly
- Use strong random keys
Examples:
- AWS Secrets Manager
- HashiCorp Vault
- Kubernetes Secrets
Attack 7: Replay Attack¶
Suppose someone captures a valid JWT.
They replay it multiple times.
The server cannot distinguish the original request from the replay.
Mitigation¶
Usually handled through:
- Short-lived access tokens
- HTTPS
- Token expiration
- Refresh token rotation
Interviewer's Perspective¶
A common interview question:
If I steal your JWT, can I impersonate you?
Answer:
Yes.
JWT is a bearer token.
Whoever possesses it can use it until it expires or is revoked.
14. HS256 vs RS256¶
Almost every senior backend interview asks this question.
HS256¶
HS256 uses a single shared secret.
The same secret is used for:
- Signing
- Verification
Advantages¶
- Simple
- Fast
- Easy to implement
Disadvantages¶
Every service that verifies JWT must know the secret.
If one service is compromised,
an attacker can generate valid tokens.
RS256¶
RS256 uses two keys.
Only the Authentication Service owns the private key.
Every other service has only the public key.
Advantages¶
Compromising an API service does not expose the signing key.
This makes RS256 a better choice for distributed systems.
Disadvantages¶
- More complex
- Slightly slower
- Requires key management
Which Should You Use?¶
Small Application¶
Perfectly acceptable.
Microservices¶
Authentication Service
↓
Private Key
↓
API Gateway
↓
Order Service
↓
Payment Service
↓
Inventory Service
↓
Public Key
RS256 is usually preferred.
Interviewer's Perspective¶
Don't answer:
RS256 is more secure.
Instead explain:
RS256 separates signing and verification.
Only one service signs tokens.
Every other service only verifies them.
15. JWT in Microservices¶
Suppose your application has:
Without JWT,
every service needs to query a centralized session store.
This creates unnecessary network calls.
With JWT¶
Each service can authenticate requests independently.
API Gateway¶
Many organizations authenticate requests at the API Gateway.
Backend services receive already authenticated requests.
Some organizations still verify JWT inside services for defense in depth.
Interviewer's Perspective¶
If asked:
Where should JWT validation happen?
A good answer:
Usually at the API Gateway.
Critical internal services may also verify tokens before processing requests.
16. Production Best Practices¶
These are the practices commonly followed in production systems.
Keep Access Tokens Short¶
Refresh Tokens Longer¶
Rotate Refresh Tokens¶
Never allow the same refresh token to live forever.
Store Refresh Tokens Securely¶
Prefer storing:
instead of the raw token.
Always Use HTTPS¶
JWT should never travel over HTTP.
Validate Everything¶
Always verify:
- Signature
- Expiration
- Issuer
- Audience
Don't rely only on signature verification.
Never Trust Client Claims Blindly¶
A valid JWT only proves the token was issued by you.
It doesn't mean the user's permissions haven't changed.
For highly sensitive operations,
many systems reload roles/permissions from the database.
Example:
instead of trusting:
inside the JWT.
Log Authentication Events¶
Useful logs include:
- Successful login
- Failed login
- Refresh token reuse
- Logout
- Multiple failed attempts
Rate Limit Authentication APIs¶
Protect:
against brute-force attacks.
17. JWT vs Session Authentication¶
| Feature | Session | JWT |
|---|---|---|
| Server Stores State | ✅ Yes | ❌ No (Access Token) |
| Horizontal Scaling | Harder | Easier |
| Logout | Simple | More Complex |
| Database Lookup Every Request | Usually Yes | Usually No |
| Mobile Friendly | Moderate | Excellent |
| Microservices | Less Suitable | Very Suitable |
| Token Revocation | Easy | Requires Strategy |
Which Should You Choose?¶
Choose Sessions when:
- Traditional server-rendered applications
- Simpler architecture
- Immediate logout required
Choose JWT when:
- REST APIs
- Mobile applications
- Microservices
- Distributed systems
Neither approach is universally better.
Choose based on your architecture.
18. How I Would Build JWT Authentication¶
This is probably the most valuable section for interviews.
If an interviewer asks:
How would you implement authentication from scratch?
A good answer could be:
Step 1¶
User submits email and password.
Step 2¶
Validate credentials.
Step 3¶
Generate:
- Access Token
- Refresh Token
Step 4¶
Store:
Step 5¶
Return:
- Access Token
- Refresh Token
inside secure HttpOnly cookies.
Step 6¶
Every protected request passes through authentication middleware.
Middleware:
- Extracts token
- Verifies signature
- Checks expiration
- Validates issuer and audience
- Attaches authenticated user
Step 7¶
Controllers use:
instead of parsing JWT again.
Step 8¶
When access token expires:
Generate:
- New Access Token
- New Refresh Token (rotation)
Invalidate the old refresh token.
Step 9¶
Logout:
Delete or revoke refresh token.
Access token expires naturally after a few minutes.
Complete Production Flow¶
Login
│
▼
Authentication Service
│
┌─────────────┴──────────────┐
▼ ▼
Access Token Refresh Token
(15 mins) (30 days)
│ │
▼ ▼
HttpOnly Cookie Database (Hashed)
│
▼
API Gateway / Middleware
│
Verify Signature
Check Expiry
Validate Claims
│
▼
request.user
│
▼
Business Logic
19. Common Interview Questions¶
Why is JWT stateless?¶
Because the server does not store access token state.
The token itself contains the information required for authentication.
Why not store passwords inside JWT?¶
JWT payload is only encoded.
Anyone can decode it.
Why use Refresh Tokens?¶
To keep access tokens short-lived without forcing users to log in frequently.
Why store Refresh Tokens in the database?¶
To support:
- Logout
- Revocation
- Rotation
- Multi-device management
Can JWT be modified?¶
Yes.
But modification invalidates the signature,
so verification fails.
Is JWT encrypted?¶
No.
It is encoded and digitally signed.
Why not use long-lived Access Tokens?¶
If stolen,
the attacker gains long-term access.
Can JWT be revoked?¶
Not directly.
Typical approaches:
- Short expiry
- Refresh token revocation
- Blacklisting
Why use HttpOnly Cookies?¶
To reduce token theft through XSS.
Why use RS256 in Microservices?¶
Only the Authentication Service owns the private key.
Other services only verify tokens using the public key.
20. Backend Cheat Sheet¶
Remember these points before an interview.
✅ Authentication verifies identity.
✅ Authorization verifies permissions.
✅ JWT is stateless because the server doesn't store access token state.
✅ JWT consists of:
- Header
- Payload
- Signature
✅ Payload is encoded, not encrypted.
✅ Signature prevents tampering.
✅ Access Tokens should be short-lived.
✅ Refresh Tokens should be stored securely and rotated.
✅ Store browser tokens in HttpOnly Secure Cookies.
✅ Protect cookies against CSRF.
✅ Protect Local Storage applications against XSS.
✅ Use HTTPS everywhere.
✅ HS256 uses one shared secret.
✅ RS256 uses a public/private key pair.
✅ Authentication belongs in middleware.
✅ Controllers should use request.user.
✅ Refresh token revocation enables logout.
✅ JWT is excellent for REST APIs and microservices.