JWTs solve one problem well — stateless verification across service boundaries. They've been cargo-culted into session management, a job they're bad at, and the mistake has real security consequences.

Somewhere in a codebase near you, a JWT is being used as a session token. It was probably chosen because "tokens" sounds like "sessions," the tutorial used one, and it worked in development. It keeps working right up until someone needs to force-log-out a compromised account and discovers there's no button that does that. JWTs are a good tool for one job and an awkward tool for the job they're often given. The two look similar enough that the mistake is common.

What a JWT actually is

Strip away the mystique and a JWT is a small, signed statement. RFC 7519 defines it in one sentence:

"JSON Web Token (JWT) is a compact, URL-safe means of representing claims to be transferred between two parties."

— IETF RFC 7519, "JSON Web Token (JWT)"

Note the words: "representing claims" between "two parties." Service A signs a set of claims — this user is authenticated, here's their ID, here are their roles — and Service B can verify that signature without calling Service A back. The value proposition is stateless verification: B trusts the token because the signature is valid. That suits service-to-service calls, OAuth access tokens and API-to-API trust. Nowhere in that design is there a concept of a session you can end.

What JWTs can't do

A session, by contrast, is stateful by nature: it exists on the server, and the server can destroy it. A JWT can't be recalled once it's issued. RFC 7519's exp claim marks the time "on or after which the JWT MUST NOT be accepted", and until then a verifier that only checks the signature and expiry will accept it — because the point was that verifiers don't have to check back with the issuer. Every problem below flows from that.

1. The logout problem

When a user clicks "log out everywhere," a session-based system deletes the session and the job is done. A JWT-based system can't delete the token — it's sitting in the user's browser and copies may be elsewhere. The only way to invalidate it early is a server-side denylist of revoked tokens (usually keyed by the token's jti identifier) that every verifier checks on every request. At that point you've rebuilt server-side session state, with extra steps, while telling yourself you're stateless.

The token lifetime decides how big that list gets, because an entry can only be dropped once the token would have expired anyway. Illustrative numbers: at 50,000 revocations a day, 15-minute tokens mean about 521 live entries at any moment. With 30-day tokens it's 1,500,000.

2. The payload is readable

JWT payloads are signed, not encrypted. The signature proves the claims weren't tampered with; it does nothing to hide them. We built a demo HS256 token and decoded its middle segment with plain base64url decoding — no key involved — and got back:

{"sub":"user-42","email":"ada@example.com","role":"admin","exp":1794297600}

That exp is 10 November 2026, 09:00 in Bratislava (CET). Anyone holding the token, or looking at browser storage, can read all of it. Paste any token into a JWT decoder and you'll see the same. The lesson: put nothing sensitive in a JWT payload. If you need confidentiality, that's JWE (encrypted), not a signed JWT. For how the three segments fit together, see JWT decoding explained.

3. The long-expiry trap

Because logout is hard, teams reach for a shortcut: set a long expiry "for user experience," 30 days say, so people don't have to log in often. Now a stolen token stays valid for 30 days with no revocation path. That's 2,880 times the exposure window of a 15-minute token.

The usual pattern is the opposite: short-lived access tokens paired with a refresh token that is stored server-side and revocable. The access token stays stateless and cheap to verify; the refresh token is where you keep the ability to say no. How short is a judgement call. For comparison, the OWASP Session Management Cheat Sheet gives common session idle timeouts of 2–5 minutes for high-value applications and 15–30 minutes for low-risk ones. Those numbers are for sessions, not tokens, but they show the scale people work at, and it isn't weeks.

4. Signing secrets are real secrets

An HMAC-signed JWT is only as trustworthy as its key. The standards are direct about this. RFC 7518 says a key "of the same size as the hash output (for instance, 256 bits for "HS256") or larger MUST be used". RFC 8725, the JWT best-practices document, adds that human-memorizable passwords "MUST NOT be directly used as the key" for HS256.

So don't use a password at all, even a generated one. Generate 32 random bytes (256 bits) with your platform's cryptographic random source and store them like any other secret; in base64url that's a 43-character string. A JWT signed with secret123 is decoration, not security. The signature itself is HMAC-SHA-256 over the header and payload; you can explore the underlying SHA-256 hash in a hash generator, though HMAC adds a key step that a plain hash doesn't.

5. Trusting the token's own algorithm

One more trap lives in verification code. RFC 8725 describes attacks where the token's header is changed to "alg": "none" and some libraries accepted it without checking any signature, or where an RSA algorithm was swapped for HMAC. The fix is to configure the verifier with the algorithm you expect and reject anything else, rather than letting the token say how it should be checked.

Common mistakes, in one list

When JWTs are exactly right

None of this means JWTs are bad. It means they're specialised. Reach for a JWT when:

For a plain web app where users log in, browse, and log out, a server-side session is very often the simpler answer — it revokes instantly and keeps its contents on the server. The gotcha isn't that JWTs are insecure. It's that "stateless" is a feature until the day you need to cancel exactly one token — and by then the design decision is already load-bearing.

← All articles