Skip to content

JWT and sessions (cookie sessions)

Introduction

A session stores "who you are" on the server and gives the browser only an ID; a JWT signs "who you are" and puts it directly in a token the client carries around.

Comparison

Cookie Session JWT
Where state lives Server (memory / Redis / database) The token itself (stored by the client)
Does the server look up storage? Yes No; verifying the signature is enough
Log someone out immediately Easy: delete the session Hard: you need an extra denylist
Multiple services / across systems Need shared session storage Convenient; each party only needs the verification key (with asymmetric signing, only the public key)
Best for Traditional websites, admin systems Mobile APIs, microservices, third-party callers

How to choose

  • Website + browser first: prefer Cookie Session—simple, revocable at any time, and the browser manages it for you.
  • Mobile, open APIs, many services calling each other: JWT fits better.
  • Do not pick JWT just because it is “fashionable”; many people mishandle its hard parts (revocation, expiry, leaks).

Using JWT safely

  1. A JWT has three parts; the middle payload is only Base64-encoded and anyone can read it, so do not put passwords, phone numbers, or other sensitive data there.
  2. The server must validate: signature, expiry (exp), issuer (iss), and audience (aud).
  3. Access tokens must be short-lived (for example 15 minutes), paired with a refresh token to get a new one; refresh tokens can be stored and revoked.
  4. Keep the signing key safe; see Secrets and configuration management.
  5. Do not accept algorithm claims such as alg: none; the server must fix the algorithm it uses.

Setting cookies safely

  • HttpOnly: frontend scripts cannot read it, which helps against XSS theft.
  • Secure: sent only over HTTPS.
  • SameSite=Lax or Strict: reduces CSRF; see Common attacks.

Component comparison

JWT access token + refresh token

Hand-rolledOpen sourceOpsMedium

After login, issue a signed "pass" (JWT) that the server need not record, since verifying the signature reveals who it is; when it expires, a separate long-lived refresh token is exchanged for a new one.

Best forBackends that provide APIs to mini-programs, apps, or other services.

Avoid whenIt is an ordinary website with no special needs; cookie sessions are easier.

Pros

The server stores no session, and every instance can verify the token

Mini-programs, apps, and third-party callers all fit well

The token can carry user information such as roles

Cons

It is hard to revoke before expiry, so a ban leaves a gap

You design how refresh tokens are stored and rotated

Stored in browser local storage, it can be stolen by script

Checklist

  • You explicitly chose session or JWT, with a clear reason
  • Cookies set HttpOnly, Secure, and SameSite
  • JWT validates signature, expiry, issuer, and audience
  • Access tokens are short-lived, with refresh and revocation
  • Tokens carry no sensitive information
  • After logout, password change, or account ban, tokens/sessions can be invalidated