Sessions vs JWT in Plain English

A session is a random ticket number your server looks up in its own records on every request. A JWT is a signed note that carries the answer inside it, so the server can read it without looking anything up. If you run one web app and one server, sessions are usually the simpler default, and easier to revoke. JWTs earn their place when something other than your own server has to verify who the user is.
You do not usually get to choose from scratch, because your auth provider already did. But your AI tool will happily write token code around it, and a few details decide whether that code is safe.
What is a session?
You sign in. The server creates a record (“session 8f3a is Maria”) and gives your browser the number 8f3a in a cookie. Every later request carries the number, and the server checks its records to see who 8f3a is.
Think of a coat check. The ticket means nothing on its own. The cloakroom knows what it points to.
What is a JWT?
A JWT (JSON Web Token) is the other way round. After sign-in the server hands back a note that says “this is Maria, valid until 3:15”, and signs it with a secret key. On later requests the server checks the signature, trusts the contents, and never looks anything up.
Think of a passport. The doorman does not phone your home country. The stamp is the proof.
Why does logout decide it?
Because a signed note stays valid until it expires, whether or not you still want it to be.
With a session, “sign Maria out everywhere” is deleting rows. The next request with a stolen cookie fails. With a plain JWT, the server has no list to delete from. The token works until its exp time, unless you build a deny list, and at that point you have rebuilt a session store with extra steps.
Supabase’s own docs show how this plays out in a JWT-based product. Access tokens are short lived (5 minutes to 1 hour, default 1 hour), and the real session lives in a database table. If you need to know a user signed out, the docs tell you to check that the token’s session_id claim still matches a row in auth.sessions. In other words, the JWT alone could not tell you, so the check goes back to a lookup.
| Session cookie | JWT | |
|---|---|---|
| Server remembers it | Yes, in a database or store | No (until you add a deny list) |
| End a stolen login | Delete the row | Wait for exp, or keep a deny list |
| Can another service verify it | Only by asking your server | Yes, with the public key (asymmetric signing) or the shared secret (HS256) |
| Easiest when | One app, one server | Several services, a mobile app, or a third-party API |
Where does the token live in the browser?
This is the part that goes wrong more often than the choice itself. If JavaScript can read a token, any script that runs on your page can steal it, including an injected one or a compromised npm package. MDN’s session management guide recommends cookies with the HttpOnly attribute for exactly that reason: even a successful XSS attack cannot read them.
There is a wrinkle that a lot of tutorials skip. Supabase’s server-side setup keeps the session in cookies, but its docs say making them HttpOnly is not necessary, because the browser-side library needs the refresh token to keep the session alive. That is a real tradeoff, made on purpose, and it means your protection against XSS has to come from elsewhere (not injecting untrusted HTML, being careful with third-party scripts). If your AI tool “fixes” a login bug by switching a cookie to HttpOnly, the browser-side Supabase client can no longer read the session it needs, so undo it.
For a custom session you write yourself, the safe baseline from MDN is: Secure, HttpOnly, SameSite=Lax or stricter, and an idle and an absolute expiry.
Three Supabase details that surprise people
- Sessions never expire by default. Per the sessions docs, a session lasts until the user signs out or something terminates it, with no limit on devices. A time limit or inactivity timeout is a Pro plan setting. Even then, the check only runs when the session refreshes, so the real lifetime is the setting plus the access token’s lifetime.
signOut()in JavaScript signs out every device. The default scope isglobal, which revokes the refresh tokens for all of that user’s sessions. Other devices fail on their next refresh with an “Invalid Refresh Token” error. If your app expects each device to stay independent, pass{ scope: 'local' }.- A password change may or may not end other sessions. The sessions docs list “the user changes their password or performs a security sensitive action” among the things that terminate a session, “depending on configuration”. Do not assume. After a password change, test whether an old device is still signed in.
If your AI wrote the JWT code itself
Check these two things first.
- The verify call names its algorithm. In
jsonwebtokenversions up to 8.5.1, a verify call with noalgorithmsoption and an empty secret could accept an unsigned token. An “empty secret” is exactly what you get whenprocess.env.JWT_SECRETis not set on a new deploy. Version 9.0.0 removed that default. Pin the algorithm anyway:jwt.verify(token, secret, { algorithms: ["HS256"] }). - The secret exists, and it is long. Make the app fail to start if the secret is missing, not fall back to a default string like
"secret"that the AI wrote into the example.
Turn it into a test
Save this as verify.test.js in a scratch folder, run npm i [email protected] (an old version with a known advisory, so scratch folders only), and run node --test (Node 18 or newer). Swap in your app’s own verify function for the risky one. The tests check behavior (a bad token is never accepted as a user), not which library call you used, so they still protect you after the AI swaps libraries. The bad conditions are set explicitly, so your own .env file cannot change the result.
const test = require("node:test");const assert = require("node:assert");const jwt = require("jsonwebtoken");
const SECRET = "a-long-random-secret-for-tests-only";const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url");// Unsigned token built by hand: header {"alg":"none"}, no signatureconst forged = `${b64({ alg: "none", typ: "JWT" })}.${b64({ sub: "admin" })}.`;
// Paste YOUR app's verify function here. This is the risky version:function verifyToken(token, secret) { return jwt.verify(token, secret);}
// The check that matters: a bad token must never come back as a logged-in user.// "Refused" means it throws, or returns nothing.function refused(fn) { try { return !fn(); } catch { return true; }}
test("an unsigned token is refused when the secret is unset", () => { assert.ok(refused(() => verifyToken(forged, undefined)));});test("an unsigned token is refused when the secret is an empty string", () => { assert.ok(refused(() => verifyToken(forged, "")));});test("an unsigned token is refused when a real secret is set", () => { assert.ok(refused(() => verifyToken(forged, SECRET)));});test("a token signed with a different secret is refused", () => { const other = jwt.sign({ sub: "admin" }, "someone-elses-secret"); assert.ok(refused(() => verifyToken(other, SECRET)));});test("an expired token is refused", () => { const old = jwt.sign({ sub: "admin", exp: Math.floor(Date.now() / 1000) - 60 }, SECRET); assert.ok(refused(() => verifyToken(old, SECRET)));});test("a good token still works", () => { const good = jwt.sign({ sub: "alice" }, SECRET, { expiresIn: "5m" }); assert.equal(verifyToken(good, SECRET).sub, "alice");});Run on jsonwebtoken 8.5.1 (Node 22), the risky version fails exactly the two tests where the secret is missing or empty. That failure is the bug: the forged token was accepted.
not ok 1 - an unsigned token is refused when the secret is unsetnot ok 2 - an unsigned token is refused when the secret is an empty string# pass 4# fail 2Now pin the algorithm and check the secret:
function verifyToken(token, secret) { if (!secret) throw new Error("JWT_SECRET is not set"); return jwt.verify(token, secret, { algorithms: ["HS256"] });}Same tests, same old library version: # pass 6, # fail 0. Each part also holds on its own. With only the pinned algorithm and no missing-secret check, the same six tests pass on 8.5.1, so the pin alone refuses the unsigned token even with no secret. On jsonwebtoken 9.0.2 the original one-line version also passes all six, because it rejects the forged token with the message please specify "none" in "algorithms" to verify unsigned tokens. So the upgrade fixes it, and the pinned algorithm fixes it on any version. The last three tests (a different secret, an expired token, and a good token) guard the other common mistakes, such as adding ignoreExpiration: true to quiet a login bug.
A prompt to paste into your AI tool
Show me exactly how this app identifies a signed-in user on the server. Is it a session cookie looked up in a store, or a JWT? If a JWT: where is it stored in the browser, which library verifies it, are the allowed algorithms pinned, what happens if the signing secret is missing, how long does it live, and what happens to it when the user logs out? Do not change any code yet.
The check an engineer does by hand
Sign in, copy your session token or cookie value, sign out, then use the copied value in a request. For a session cookie, it should fail at once. For a JWT, it can keep working until it expires. Supabase’s signout docs say so about their own tokens: access tokens of revoked sessions stay valid until the time in their exp claim. The sessions docs add that you can check the token’s session_id claim against a row in auth.sessions, and that no row means the user signed out. You could do that check in a database policy on sensitive tables, at the cost of one extra lookup per query. Worth it for money and admin data, not for a to-do list.
The sign-in side of this lives in auth for vibe coders. The “what is this person allowed to do” side is the same problem row level security solves for data. The difference between the two has its own post: authorization vs authentication.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.