Basics

Session or JWT — which login should your app use

Illustration: a ledger of entries on one side, a lanyard pass on the other

The "sessions or JWT" argument looks like a choice between old and trendy. It's really a different choice: where the truth about you being logged in lives — on the server, or in your pocket.

And that choice has one consequence people usually discover late: only one of the two lets you log someone out instantly.

Two ways to remember you're logged in

Session. You typed your password. The server wrote a row: "id abc123 is Marina, signed in at 14:20". All it handed you is abc123 — usually in a cookie. On every request the server takes that number and looks up its own record.

The truth lives on the server. You hold a coat-check tag.

JWT. You typed your password. The server handed you a token that says inside "Marina, role admin, valid until 15:20" and signed it with its key. On every request the server verifies the signature — that's all. It goes nowhere.

The truth lives with you. The server only keeps a key to check the signature. The token's anatomy is covered in the JWT explainer.

Compared on what actually hurts

  • Checking each request. A session means a lookup in storage (usually Redis or the database). A JWT is just signature math, no lookup. That's the main argument for tokens.
  • Instant logout. Session — delete the row and access ends that second. JWT — the token in the user's hands works until it expires, and nobody asks the server.
  • Changing permissions. Demote an admin to a normal user: a session picks it up on the next request, a JWT keeps carrying the old admin role until it expires.
  • Size. A session id is tens of bytes. A token with data inside is hundreds of bytes or a kilobyte — and it rides along on every request.
  • Working across services. Sessions need shared storage. A token can be verified by any service holding the public key — that's its real strength.
  • Cost of theft. A stolen session id dies the moment you delete the row. A stolen token stays alive until expiry, whatever you do.

The trap the whole argument is really about

People choose tokens to avoid hitting the database on every request. Now watch what happens next.

A user clicks "sign out everywhere". Or you offboard an employee. Or you spot a breach. The issued token has to stop working right now.

There's exactly one way to do that: keep a list of revoked tokens and check against it. And here OWASP says precisely what you were afraid of: once an application implements such a deny list, "user sessions won't be completely stateless anymore which might defeat the benefits of stateless sessions."

Which means you've arrived exactly where you started: a lookup on every request, only more complicated.

The standard mitigation is a short-lived token (5–15 minutes) plus a refresh token that renews access — and that one is stored server-side and can be revoked. It's a solid design, but be honest about it: that's a session wrapped in tokens.

Who should pick what — no fence-sitting

One app, one server, an ordinary website — take sessions. They're easier to debug, logout is instant, and at your scale you won't notice the "extra" storage lookup. It's the boring, correct choice for 90% of first projects.

Several independent services, or a mobile app plus an API — take JWT. Here its strength is real: a service needs no shared storage, just the key to verify a signature. But build in a short lifetime and a refresh token from day one, not a 30-day token.

Not sure? Don't choose. Take a ready-made auth service: Supabase Auth, Auth0, Clerk. Inside is almost always that same short-JWT-plus-refresh pair, already assembled correctly. Wiring one up is covered in the step-by-step login guide.

There's exactly one bad option: a homemade 30-day JWT with no way to revoke it. You can neither cancel it nor update its permissions — which is precisely what you'll need on the first bad day.

Which is more secure, a session or a JWT?

On their own, roughly equal — both are standard designs. The difference is what theft costs: a session can be killed, a token has to be waited out. By that logic sessions forgive mistakes better.

Where should I store the token on the client?

In an httpOnly cookie, not localStorage: a rogue script on the page can't reach a cookie with that flag. As a bonus the browser sends it automatically, so you don't hand-roll a header.

Is a JWT about authentication or authorization?

Both at once, which is what confuses people. The token proves who you are (authentication) and often carries roles (authorization). The difference between those two words is unpacked in authentication vs authorization.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →