Skip to the guide

Sessions, cookies and JWT

IntermediateConcept3 pages

After login, every request proves "this is user 7": with a random session ID that points at a record on the server, or a signed token that carries the facts itself, usually a JWT. Stateless services and sessions covered where that state lives. This guide covers the mechanics interviewers probe: cookie settings, JWTs, refresh tokens, and where a browser keeps them.

A cloakroom ticket and a school-trip slip

At a theatre cloakroom you get a numbered ticket. The number means nothing on its own; your coat hangs on the rack behind the counter. Lose the ticket and the attendant can cancel that number at once.

A school trip works differently. The head teacher signs a slip: "Sam Lee may join the museum trip, Tuesday only." The bus driver and the museum guard each check the signature without phoning the school. Anyone can read it. And once it's handed out, the school can't take it back before Tuesday ends.

  • the cloakroom number → a session ID: random, meaningless without the session store behind it
  • cancelling the number → logout takes effect at once
  • the signed slip → a JWT: the facts (user, roles, expiry) written down and signed
  • checking the head's signature → verifying with the auth service's public key, no call needed
  • "Tuesday only" → the exp claim; and the slip is readable by anyone, so nothing secret goes inside