Stackvora.tech

Authentication: JWT vs Sessions

When to use JWTs vs server-side sessions — with real security tradeoffs and 2026 recommendations.

The "JWT vs sessions" debate is 10 years old and still misunderstood. The short answer: use server-side sessions for browser apps unless you have a specific reason not to. Here is why.

How each works

  • Session — server stores state, browser holds an opaque cookie referencing that state. Lookup on every request.
  • JWT — server issues a signed token containing the state. Browser sends it back. Server verifies the signature and trusts the payload.

The comparison table nobody puts in the docs

ConcernSessionJWT
RevocationInstant — delete rowHard — needs a blocklist
ScaleNeeds sticky session or shared storeStateless
Payload sizeSmall cookieLarger (headers grow)
Cross-service authCentral checkAny service verifies
XSS impacthttpOnly cookie — safeUsually LS/JS-accessible — leaked
CSRF riskYes — needs mitigationLower (with header transport)

Rules of thumb (2026)

  • Web app, single backend → session cookie, httpOnly + Secure + SameSite=Lax.
  • Mobile app talking to your own API → short-lived JWT access token + rotating refresh token stored in the OS keychain.
  • Microservices or federated auth → JWT for service-to-service, session for the user-facing edge.
  • Public API for third parties → OAuth 2.1 with PKCE, JWT access tokens.

The three biggest JWT mistakes

  1. Storing JWTs in localStorage. XSS = complete account takeover.
  2. Using long-lived JWTs (days or weeks) with no refresh rotation.
  3. Trusting the alg header — force a specific algorithm on the verify side (none attacks still exist in the wild).
Don't pick JWT because it's fashionable. Pick the mechanism whose failure mode you can live with.

Back to the knowledge base · Ask the AI assistant