Sessions vs JWT vs Cookies: Understanding Authentication Approaches
What sessions are => A session is a way for the server to remember a user.
How it works:
User logs in
Server creates a session (stores user data on server)
Server sends a session ID to the browser
Browser sends that session ID with every request
Server checks it and recognizes the user
π Think of it like a cloakroom token:
Your data is stored safely (on server)
You just carry a small token (session ID)
What cookies are => A cookie is a small piece of data stored in the browser.
Key points:
Sent automatically with every request
Can store things like:
Session ID
Preferences
Login info (not sensitive data ideally)
π Important:
Cookies are just storage, not authentication itself.What JWT tokens are => JWT = JSON Web Token
Itβs a self-contained token that stores user information.
Structure:
Header
Payload (user data)
Signature (security)
How it works:
User logs in
Server creates a JWT
Sends it to client
Client stores it (usually in localStorage or cookie)
Client sends JWT with every request
Server verifies it (no DB lookup needed)
π Think of JWT like a digital ID card you carry yourself.
Stateful vs stateless authentication => π§ Stateful (Sessions)
Server stores user data
Needs memory/database
Tracks each user session
β‘ Stateless (JWT)
Server stores nothing
All info is inside token
Faster and scalable
Differences between session-based auth and JWT =>
Feature Session-Based Auth π§ JWT-Based Auth β‘ Storage Server Client State Stateful Stateless Speed Slower (DB lookup) Faster Scalability Harder Easier Security Very secure (server-side) Depends on storage Token Size Small (just ID) Larger (data included) Logout Handling Easy Hard (token still valid) Use in APIs Not ideal Very popular When to use each method =>
β Use Sessions When:
You are building a traditional website
You need strong control over users
You want easy logout handling
Example:
Banking apps
Admin dashboards
β Use JWT When:
You are building APIs or mobile apps
You need scalability
You have microservices architecture
Example:
REST APIs
Mobile backend
Single Page Apps (React, Angular).
