As web applications grew beyond simple login pages, the cracks in classic session-based authentication began to show.
Maintaining millions of active user session in a single centralized store became a scaling nightmare.
Servers had to constantly synchronize session data, and horizontal scaling (adding more servers) became complicated.
This is when the token revolution began – a shift toward stateless authentication, where the servers no longer needs to remember who you are.
The Core Idea – Make Authentication Stateless
In classic authentication, a user's session data was stored in the server, and the browser only kept a tiny session ID cookie.
In token-based systems, the idea flipped:
The client now carries the user's identity – in the form of a token – and the server verifies it using cryptographic signatures.
This simple shift removed the need for a centralized session store, which made authentication scalable, distributed, and API-friendly.
How Tokens Work
When a user logs in:
- The Authentication Server verifies their credentials.
- It then issues a token, which contains encoded user information (claims).
- The token is sent to the client (web, mobile, or API consumer).
- On every request, the client sends the token (typically in the HTTP header).
- The server verifies the token's signature and grants access if valid.
The Rise of JWT (JSON Web Tokens)
The most popular implementation of this idea is the JWT (JSON Web Token).
A JWT is a compact, URL-safe token composed of three parts:
Header.Payload.Signature
For example:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJ1c2VySWQiOiIxMjMiLCJyb2xlIjoiYWRtaW4ifQ.
TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
Parts Explained:
- Header: Specifies the algorithm (e.g., HS256)
- Payload: Contains claims like user ID, email, role, or expiry
- Signature: Cryptographic verification that ensures integrity
Access Tokens and Refresh Tokens
Step 1: Login (Get Both Tokens)
When you first log in:
- You send your credentials (username + password) to the Auth Server.
- The Auth Server checks and gives you:
- An Access Token (short lived)
- A Refresh Token (long-lived)
Step 2: Using the Access Token
Whenever you make an API request:
You include the access token in the header, like this:
Authorization: Bearer <access_token>- The API server verifies it and lets you access protected data.
But – access token expiry quickly (like 15-30 minutes).
That's for security reasons – if someone steals it they can't use it for long.
Step 3: When It Expires (Use Refresh Token)
When your access token expires:
- The app sends the refresh token to the auth server.
- The server checks if it's still valid.
- If valid -> it gives you new access token (and maybe a new refresh token).
- You can now continue using the app without logging in again.
Why Two Tokens?
Because they serve different purposes:
| Token Type | Lifespan | Stored Where | Purpose | Risk |
|---|---|---|---|---|
| Access Token | Short (15 min – 1 hr) | Client (memory/localStorage) | Used to call APIs | If stolen, attacker can use it for a short time |
| Refresh Token | Long (days/weeks) | Secure storage (httpOnly cookie or keychain) | Used to get new access tokens | If stolen, attacker can keep refreshing — dangerous |
This separation provides:
- Better security: short access tokens limit damage.
- Better user experience: you don't need to log in repeatedly.
Using Refresh Tokens:
- Access token short-lived – limits damage if stolen.
- Refresh token used to obtain new access token when the access token expires.
- Refresh token are sensitive and must be stored/used securely.
- Refresh token rotation: issue a new refresh token on each use and revoke the old one to prevent replay attacks.
Real Example
Imagine you are using Spotify:
- You log in once today.
- The access token allows the mobile app to call APIs (like “get playlist”) for the next 30 minutes.
- After 30 minutes, the app silently uses your refresh token to get a new access token – you don't notice everything.
- You only have to re-login when your refresh token expires (after days or weeks).
Leave a comment
Your email address will not be published. Required fields are marked *
