What it is
A session is a period of recognized interaction, often represented by a server-side record. A token is a value presented as evidence of authority. An opaque random token can identify that session; a JWT can carry signed claims. A cookie is a browser storage and transport mechanism, not a competing kind of identity.
Why you need it
You do not want to resend a password on every request. A limited credential lets later requests identify a session, while giving the application a lifecycle it can expire or revoke.
How it works
- The server verifies a login.
- It creates a session record and an unpredictable session identifier.
- The browser stores the identifier in a protected cookie.
- Later requests present that identifier; the server checks the corresponding session.
Two designs with different validation work
Opaque token: random-session-id
Server record: { userId, expiresAt, revokedAt }
JWT claims: { sub: "user-7", iss: "issuer", aud: "api", exp: ... }
Server work: verify signature + expected claims + authorizationA signed JWT is generally readable, not automatically encrypted. Both approaches require safe issuance, transport, lifetime rules, and permission checks. The presence of a valid signature does not itself prove the session is still active.
Download the file and run node backend-auth.mjs with Node.js 22 or newer. It tests an in-memory auth model; it is not a production login server.
Good practices and why
- For a browser app, consider a server-managed session before accepting the added lifecycle complexity of distributed tokens.
- Treat bearer credentials like temporary keys: whoever holds one may be able to use it.
Common mistakes
- Calling every token a JWT.
- Putting secrets into a signed JWT because its contents look encoded.
Try it yourself
Draw one request using an opaque session cookie and another using a signed access token. Label what is stored by the client and what the server validates.
What a successful result looks like
Your diagrams separate token format, transport, session state, and authorization rather than treating them as the same thing.
0 / 6000 characters · Saved on this device. Do not include passwords or private data.
Check your understanding
Answer the understanding check correctly to record exercise completion.
Related interview practice
These scenarios add constraints and follow-up questions to the concepts you just learned.
Further reading
- OWASP: authentication ↗
- OWASP: password storage ↗
- OWASP: session management ↗
- MDN: Set-Cookie ↗
- IETF: JWT best current practices ↗
Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.