Every login system eventually picks one of two shapes. Either the server keeps a session record and hands the browser a cookie holding nothing but an identifier, or the server issues a JSON Web Token that carries its claims with it. Both are widely used, both work, and the arguments between their supporters often hide the real trade-offs.
This guide compares the two plainly: where the state lives, what revocation costs, how each one scales, and how each is exposed to the two browser attacks that matter here, XSS and CSRF. No statistics and no default winner: just the mechanics that should drive the choice. If you have a token of your own, the JWT Decoder on Ovatly shows what it carries as you read.
Contents
- Quick answer
- Where the state lives
- Revocation: ending a session on purpose
- Scaling and server lookups
- XSS and CSRF exposure
- Inspecting a token while you decide
- Limitations of this comparison
- Common mistakes
- FAQs
- Conclusion
Quick answer
Choose session cookies when one server-side application serves your users and you want simple, immediate control: the state lives on your server, and deleting the session ends it. Choose JWTs when several services need to read the same claims without asking a central session store on every request, and accept that a token is hard to revoke before it expires. In both cases the security work is in the details: how the cookie or token is stored, how long it lives, and whether the receiving server actually verifies what it is given. If you are evaluating a token-based design, decode a sample token with the JWT Decoder and read what it carries first.
Where the state lives
A session cookie is a pointer. The browser holds a random session identifier, and everything meaningful (the user id, the login time, the permissions snapshot) lives in a server-side record. The cookie itself says nothing; it only names a row.
A JWT is the opposite arrangement. The payload carries the claims directly: sub for the subject, iss for the issuer, iat for the issued-at time, exp for the expiry, and whatever custom claims the issuer added. The server that receives the token can read those claims straight from the token after checking the signature, without consulting a session store. That self-contained quality is the entire appeal, and also the source of every drawback that follows.
Revocation: ending a session on purpose
Sooner or later you need to end access deliberately: a user logs out everywhere, a password changes, an account is suspended, a device is lost.
With server sessions this is trivial. Delete or flag the session record and the next request with that cookie finds nothing. The identifier in the browser becomes worthless immediately, because the state it pointed to is gone.
With a JWT there is no record to delete. The token is valid because its signature checks out and its exp claim has not passed, and both of those facts travel with the token itself. Revoking one early means keeping a blocklist of revoked tokens, which reintroduces exactly the server-side lookup the design was avoiding, or keeping token lifetimes short and leaning on refresh mechanisms. Neither is impossible, but revocation is a feature you add to a token design, whereas it is the default behaviour of a session design.
Scaling and server lookups
The session model costs a lookup on every request. For a single application with a fast session store, that cost is small and predictable, and it buys the revocation behaviour above. The friction appears when many separate services all need the session and all need the same store.
The token model removes the lookup. Any service holding the issuer's public key can verify a token and read its claims locally, which is why tokens are popular across APIs and microservices. The price is paid elsewhere: claims go stale, because a token keeps asserting whatever was true when it was issued until it expires, and every service must implement verification correctly, every time.
XSS and CSRF exposure
Neither mechanism is safe by default; each has a characteristic exposure.
A token that JavaScript can read can be stolen by JavaScript. If a cross-site scripting flaw lets attacker code run on your page, a token stored in local storage can be copied and replayed from anywhere until it expires. Putting the token in an httpOnly cookie instead keeps scripts from reading it, which trades away some of the token model's flexibility.
Cookies have the mirror-image problem. A browser sends cookies automatically with requests to their domain, which is exactly what cross-site request forgery exploits: a malicious page tricks the browser into making a request your server trusts because the session cookie rode along. Defences exist, such as SameSite attributes and anti-forgery tokens, but they are part of the design work, not a bonus. A token sent in an Authorization header is not attached automatically by the browser, so that particular forgery route does not apply to it.
Inspecting a token while you decide
If you are weighing a token design, look at a real token before you argue about it in the abstract. Paste one into the JWT Decoder and work through what it shows you:
- Read the decoded header, which names the algorithm and token type.
- Read the payload claims: subject, issuer, issued-at and expiry, plus any custom claims the issuer added.
- Check the expiry panel for the readable date and the verdict. A distant exp means a long window for a stolen token; no exp at all means the token has no expiry of its own.
- Remember the signature warning: the decoder labels the signature as not verified, because decoding is not verification.
The demo token in the tool makes the pattern concrete: its payload carries sub "1234567890", name "John Doe" and iat 1516239022, and adding an exp of 4000000000 makes the tool report expiry on 2 October 2096, an expiry your future self has to live with, because you cannot call that token back.
Limitations of this comparison
This is a comparison of mechanisms, not a security audit of any product, and real systems routinely mix the two: a session cookie for the web app, tokens for the API, short-lived access tokens backed by server-held refresh sessions. Frameworks also differ in their defaults, so the same choice can be safe in one stack and sloppy in another. The decoder's role in all of this is narrow: it reads tokens for inspection, locally in your browser, and never verifies signatures. Any claim in this guide about what a token proves assumes a server that verifies properly with the signing key or the issuer's public key.
Common mistakes
- Choosing tokens to avoid a session store, then adding a blocklist. You have rebuilt the store with extra steps. Decide whether revocation matters before choosing.
- Setting a long exp and forgetting it. The expiry claim is the only built-in limit on a token's life. Tokens without an exp claim have no expiry of their own, which should be a deliberate decision, never an oversight.
- Storing tokens where any script can read them. That placement hands the token to the first XSS flaw. Storage location is a security decision.
- Assuming cookies are the unsafe old option. Their automatic sending is a real exposure, but it is an understood one with standard defences, and their revocation behaviour is the best available.
- Trusting a decoded payload. A token you decoded is a set of claims. Your server's signature check is what makes them trustworthy, and a decoder page cannot do that check for you.
FAQs
Can the JWT Decoder verify that a token is real? No, and be suspicious of any decoder that implies it can. Verification needs the secret or public key used to sign the token, which a decoder page does not have. This tool decodes and labels the signature as not verified.
Why does my token say expired when the app still accepts it? Some systems allow clock skew leeway of a minute or two, and some ignore exp for specific flows. The tool reports the claim exactly; your server's policy decides what is enforced.
Is it safe to paste a real token here? The decoding runs entirely in your browser and nothing is sent anywhere. Still, treat tokens like passwords generally: do not post them in public places or screenshots.
What if my token has no expiry? Tokens without an exp claim are reported as having no expiry of their own, which is common for some API tokens. Skipping exp gives up the one built-in limit on how long a stolen token stays useful.
What are sub, iss and aud? Registered claim names: sub is the subject (usually the user id), iss is the issuer, aud is the intended audience, exp the expiry, iat the issued-at time.
Conclusion
The choice is really about where you want the state and who pays for revocation. Sessions keep state on the server and make ending access easy, at the cost of a lookup per request. JWTs carry their claims and verify anywhere, at the cost of revocation and freshness. Pick based on which cost your system feels more, then do the unglamorous work: short lifetimes, careful storage, and servers that actually verify. Read any token first with the JWT Decoder; if the claim names are unfamiliar, start with the claim-by-claim walkthrough.
Related guide: How to Read the Claims Inside a JWT (iss, sub, aud, exp).
More tools for this kind of work live in the Developer Tools archive.