LEARN. PRACTICE. EXPLAIN.
Learning paths & interview practiceP
Backend foundations
LESSON 3 OF 8 · ABOUT 14 MINUTES + EXERCISE

What is a session? What is a token?

THE IDEA

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

  1. The server verifies a login.
  2. It creates a session record and an unpredictable session identifier.
  3. The browser stores the identifier in a protected cookie.
  4. Later requests present that identifier; the server checks the corresponding session.
Data model

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 + authorization

A 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.

Try the local authentication lab

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.

Download runnable lab

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.
GUIDED IMPLEMENTATION

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

Which statement is correct?

Answer the understanding check correctly to record exercise completion.

PUT THE IDEA UNDER PRESSURE

Related interview practice

These scenarios add constraints and follow-up questions to the concepts you just learned.

Further reading

Selected primary references · reviewed 2026-10-09. Examples are educational; use your framework’s documented integration guidance when building a deployed system.

AlifBay Prep