← Back to list

Sessions vs JWT vs Cookies

Understanding Authentication Approaches

Bhavay Nagpal in Towards Dev · 2026-05-19 07:19 · 0 claps · 4.2 min read
#javscript #backend #web-development #authentication #coding
Open on Medium ↗
Wiki topics: 💻 · Programming 🌐 · Web Development

Sessions vs JWT vs Cookies

Understanding Authentication Approaches

Introduction

Authentication is one of the first major backend concepts every web developer encounters. And almost immediately, three terms start appearing everywhere:

  • Sessions
  • Cookies
  • JWT Tokens

Most beginners confuse them because they are closely related, but they solve different problems.

This article breaks them down in a practical way so you understand:

  • What each one actually does
  • How they work together
  • Stateful vs stateless authentication
  • When to use sessions
  • When to use JWTs
  • Common misconceptions developers have

What Are Cookies?

A cookie is a small piece of data stored in the browser.

The server sends cookies to the client, and the browser automatically sends them back with future requests.

Cookies are not authentication systems by themselves.

They are simply a storage and transport mechanism.

Example

When you log into a website:

  1. Server verifies your credentials
  2. Server sends a cookie
  3. Browser stores the cookie
  4. Browser automatically includes it in future requests

What Cookies Usually Store

Cookies can store things like:

  • Session IDs
  • JWT tokens
  • User preferences
  • Theme settings
  • Language settings

Important Thing to Understand

Cookies are just containers.

They can carry:

  • Session IDs
  • JWTs
  • Other small pieces of data

A cookie itself is not the authentication strategy.

What Are Sessions?

Sessions are a stateful authentication approach.

With sessions, the server stores user authentication data.

The browser only stores a session ID.

How Session Authentication Works

Step-by-Step Flow

1. User logs in
2. Server validates credentials
3. Server creates a session
4. Session is stored on server
5. Server sends session ID inside cookie
6. Browser stores cookie
7. Browser sends cookie with every request
8. Server finds session using session ID
9. User is authenticated

Session Authentication Flow Diagram

┌──────────┐
│  Client  │
└────┬─────┘
     │ Login Request
     ▼
┌──────────┐
│  Server  │
└────┬─────┘
     │ Creates Session
     │ Stores User Data
     ▼
 Session Store
     │
     │ Sends Session ID Cookie
     ▼
┌──────────┐
│ Browser  │
└────┬─────┘
     │ Future Requests + Cookie
     ▼
┌──────────┐
│  Server  │
└──────────┘

Example Session Data

Stored on the server:

{
  sessionId: "abc123",
  userId: 42,
  role: "admin"
}

Stored in browser cookie:

sessionId=abc123

Advantages of Sessions

Disadvantages of Sessions

What Are JWT Tokens?

JWT stands for JSON Web Token.

JWT authentication is usually a stateless authentication approach.

Instead of storing authentication data on the server, the server stores it directly inside the token.

The client stores the token and sends it with requests.

JWT Structure

A JWT has three parts:

HEADER.PAYLOAD.SIGNATURE

Example:

eyJhbGciOiJIUzI1Ni...

JWT Payload Example

{
  "userId": 42,
  "role": "admin",
  "exp": 1762345678
}

How JWT Authentication Works

Step-by-Step Flow

1. User logs in
2. Server validates credentials
3. Server creates JWT
4. JWT sent to client
5. Client stores JWT
6. Client sends JWT with requests
7. Server verifies JWT signature
8. User is authenticated

JWT Authentication Flow Diagram

┌──────────┐
│  Client  │
└────┬─────┘
     │ Login Request
     ▼
┌──────────┐
│  Server  │
└────┬─────┘
     │ Creates JWT
     ▼
┌──────────┐
│ Browser  │
└────┬─────┘
     │ Sends JWT
     ▼
┌──────────┐
│  Server  │
└──────────┘

Where JWTs Are Stored

JWTs are commonly stored in:

  • Cookies
  • localStorage
  • sessionStorage

This is where many beginners get confused.

JWT and cookies are not competitors.

A JWT can be stored inside a cookie.

Stateful vs Stateless Authentication

This is the core difference between sessions and JWTs.

Stateful Authentication

With stateful authentication:

  • Server remembers users
  • Authentication data stored server-side
  • Sessions are stateful

Example:

Server memory/database stores session information

Stateless Authentication

With stateless authentication:

  • Server does not store login state
  • Token contains authentication data
  • JWT authentication is usually stateless

Example:

Server only verifies token signature

Session vs JWT Comparison Table

Sessions vs Cookies vs JWT

This is the easiest way to think about them:

Common Beginner Misconceptions

Misconception 1: Cookies Are Authentication

Wrong.

Cookies are just storage.

Authentication logic comes from sessions or JWT validation.

Misconception 2: JWT Replaces Cookies

Not always.

JWTs are often stored inside cookies.

Misconception 3: JWT Is Always Better

No.

JWTs solve scaling and distributed architecture problems.

For many applications, sessions are simpler and safer.

When Should You Use Sessions?

Sessions are usually a good choice for:

  • Traditional web applications
  • Admin dashboards
  • Monolithic backend systems
  • Apps requiring strict logout control
  • Applications with server-rendered pages

Real-World Examples

  • Banking dashboards
  • Internal company tools
  • CMS platforms
  • Admin panels

When Should You Use JWT?

JWTs are usually better for:

  • Mobile applications
  • Public APIs
  • Microservices
  • Distributed systems
  • Multi-platform authentication

Real-World Examples

  • Mobile app backends
  • Third-party APIs
  • SaaS platforms
  • Authentication gateways

Which One Should Beginners Learn First?

Learn sessions first.

Why?

Because sessions teach:

  • Request lifecycle
  • Cookies
  • Server-side authentication
  • Stateful systems

Once sessions make sense, JWT becomes much easier to understand.

Practical Industry Reality

Modern applications often combine multiple approaches.

Examples:

  • JWT stored inside HTTP-only cookies
  • Session authentication for web dashboard
  • JWT authentication for mobile app APIs

Real systems are rarely purely one thing.

Final Thoughts

Sessions, cookies, and JWTs are connected concepts, but they are not interchangeable.

The biggest thing to remember is this:

  • Cookies store data
  • Sessions store authentication state on the server
  • JWT stores authentication data inside the token itself

If you understand that distinction clearly, most authentication confusion disappears.

Authentication architecture is less about which technology is “best” and more about choosing the right tradeoffs for your application.


메타데이터
post_id
d60b23e6bdff
slug
sessions-vs-jwt-vs-cookies-d60b23e6bdff
url
https://medium.com/@bhavaynagpal000/sessions-vs-jwt-vs-cookies-d60b23e6bdff
canonical_url
https://medium.com/@bhavaynagpal000/sessions-vs-jwt-vs-cookies-d60b23e6bdff
author_url
https://medium.com/@bhavaynagpal000
status
ok
fetched_at
2026-06-09 15:37:30