Skip to the page

Authentication and authorization · core

Sessions vs JWT

A server session stores login state on the server and gives the browser only a random session id (easy to revoke), while a JWT is a signed token that carries the claims itself (no lookup needed, but hard to revoke before it expires).

The notes

What you must know

  • Session: random id (at least 64 bits of entropy) in an HttpOnly cookie -> server looks it up in a store (Redis, DB). Logout or ban = delete the session. Needs a shared store when you have several instances.
  • JWT: header.payload.signature, each part base64url. The payload is readable by anyone; the signature only proves it was not changed. Do not put secrets or personal data in it.
  • Verify every JWT: pin the allowed algorithm (reject 'none' and never let the token header choose), check signature, exp, nbf, iss and aud; get keys from a trusted source (JWKS), not from the token header.
  • Revocation is the hard part: keep JWT access tokens short-lived (minutes) and use refresh tokens, or keep a denylist keyed by jti. OWASP notes that using JWTs as sessions needs its own invalidation design.
  • Where JWTs shine: service-to-service calls, APIs used by many clients, short-lived access tokens from an identity provider (OAuth/OIDC).
  • HS256 uses one shared secret (sign and verify); RS256/ES256/EdDSA use a private key to sign and a public key to verify, so many services can verify without being able to sign.

The red pen

Common traps: mistake → fix

  • Mistake: JWTs are encrypted

    Fix: signed JWTs are only encoded; anyone can read the payload.

  • Mistake: JWTs are always better because they are stateless

    Fix: you lose easy logout and revocation; sessions are often simpler for a web app.

  • Mistake: Decoding a JWT checks it

    Fix: decoding is not verifying; always verify the signature and claims.

Easy to hard

Questions interviewers ask, easy to hard

  1. Sessions vs JWT: how do they work and when do you pick each?
  2. What is inside a JWT? Is it encrypted?
  3. How do you log a user out or ban them when you use JWTs?
  4. What is the 'alg: none' attack? Algorithm confusion?
  5. HS256 vs RS256: when does each make sense?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview