Track 07 · Lesson 3 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
Sessions versus tokens
Lesson 3 of 18· 7 min
After you log in, the server has to recognize you on every later request. It can give you a random session ID that it looks up in its own store, or a signed token it can verify without looking anything up. Each choice gives something up.
Log in, then kick yourself out
Try both modes
Not logged in
What the browser sends
(nothing yet)
What the server stores
(no sessions)
- Log in to start.
Server sessions#
The server stores “session 8f3a… = user 4821” in a database or Redis. The browser holds only the random ID, in a cookie. Revoking is easy: delete the row and the user is out, everywhere. The cost is state: every request needs a lookup in a store every server can reach.
Self-contained tokens#
A JWT carries “user 4821, role user, expires 10:00”, signed by the server. Any server with the key can check it with math, no lookup. The catch is revocation: a token stays valid until it expires, unless the server keeps a denylist, which brings the lookup back. That’s why tokens are kept short-lived.
Most apps use both ideas#
A short-lived access token for everyday calls (fast, stateless) plus a refresh token the server tracks and can revoke (like a session). The hotel below shows how the pieces fit together, and the next lessons take each one apart.
The hotel analogy
Tap a card to flip it
Tap a card to flip it to the technical version. 0 of 6 flipped.
Check yourself