The server forgets you
HTTP is stateless. Each request stands alone. When you log in to a site and then click to another page, that second request arrives with no connection to the first. As far as the protocol is concerned, you are a stranger again.
Typing your password on every click is not an option. So after a successful login the server gives the browser something to show on later requests, as proof that it already logged in.
There are two main designs for that "something".
Sessions: a ticket number
The server creates a record, the session, and gives the browser its id.
A login, and then the next request from the same browser.
On login, the server creates a random session id
A long, unguessable string. It contains no information.
It stores the id with the user's details
In a database or an in-memory store.
It sends the id to the browser in a cookie
A cookie is a small value the browser keeps for a site and sends back automatically with every request to that site.
On every request, the server looks the id up
Finding the session tells it who is asking.
It is a coat check. You hand over your coat and get a numbered ticket. The ticket is worthless to anyone who reads it. It only means something to the attendant with the rack.
Tokens: a signed pass
The other design stores nothing on the server. Instead, the server writes the facts down, signs them, and hands the whole thing to the browser.
A login, and then the next request from the same browser.
The most common format is a JWT (JSON Web Token). It has three parts: a header, the claims (who you are, when it expires), and a signature made with a key only the server holds.
On each request the server recomputes the signature. If it matches, the contents have not been altered, so they can be trusted without consulting any database.
It is a festival wristband. Staff do not look you up on a list. They look at the wristband, see that it is genuine, and read your access level straight off it.
The trade-off
A login, and then the next request from the same browser.
| Session | Token | |
|---|---|---|
| The browser holds | A meaningless id | The facts themselves, signed |
| Server-side storage | Needed, and read on every request | None |
| Many servers | All must share the session store | Any server with the key can verify |
| Log a user out immediately | Delete the session. Done | Hard: the token stays valid until it expires |
| Change a user's permissions | Takes effect on the next request | Takes effect when the token is replaced |
| Size sent with each request | Tiny | Larger |
The row that matters most is logging out. With a session, deleting the record ends it at once. With a token, there is nothing to delete. A stolen or cancelled token keeps working until its expiry time.
That is why tokens are given short lives, often minutes, paired with a longer-lived refresh token that is checked against the server when a new short one is needed. That check is where a logout or a ban takes effect.
Which to use
A normal website with its own login: sessions
Simple, instantly revocable, and one lookup per request is cheap.
Many separate services that must each trust a login: tokens
A token issued by one service can be verified by another without calling back.
Access for another company's app: tokens
This is what "Log in with ..." systems hand out.
Keeping either one safe
Whatever the browser holds is, in effect, the user's identity. Anyone who copies it becomes that user.
- Always use HTTPS, so it cannot be read in transit.
- Mark cookies
HttpOnly, so scripts on the page cannot read them. This limits the damage of a script injected into your page. - Mark cookies
Secure, so they are never sent over plain HTTP. - Set
SameSite, so other sites cannot make the browser send your cookie along with a request they trigger. - Expire them. A session that lasts for ever is a stolen laptop waiting to happen.
- Issue a new id at login, so one planted before login is useless afterwards.
Key takeaways
- HTTP does not remember users, so the browser must present proof of login on each request.
- A session id is a meaningless ticket. The server looks it up to find out who you are.
- A token carries the facts with a signature, so the server can trust it without a lookup.
- Sessions are easy to cancel and need shared storage. Tokens need no storage and are hard to cancel.
- Either way, protect it in transit and in the browser, and make it expire.