JWT vs Session Cookies: Which Should You Use for Login?
By Tools Star Hub Editorial Team · Published
Quick answer: a session cookie holds a random session ID, and the server looks up the matching login record in its own store on every request. A JSON Web Token, defined in RFC 7519, carries signed claims such as the user ID and expiry inside the token itself, so a server can check it without a lookup. The two are not strict opposites, because a JWT can be sent in a cookie too. For a typical server-rendered website, server-side sessions in a secure, HttpOnly cookie are simpler and easier to revoke. JWTs suit APIs and systems where several services need to verify identity independently. The JWT Decoder lets you inspect what a token actually contains.
Choosing how to keep users signed in affects security, performance and how easily you can log someone out. Many tutorials present JWTs as the modern default, but the trade-offs are real: a token that cannot be revoked quickly is a problem after a password change, and a token stored in the wrong place can be stolen by injected script. Knowing how each model works makes the decision much clearer.
How do server-side session cookies work?
After a user signs in, the server creates a session record containing who they are and when the session expires. It sends back only a long, random identifier in a Set-Cookie header. The browser returns that cookie automatically on later requests to the same site, and the server finds the record by its ID.
Because the real state lives on the server, the cookie value has no meaning on its own. Logging out or forcing a sign-out after a password reset is as simple as deleting the record. The cost is that every request needs a lookup, so a site with many servers needs a shared session store such as a database or an in-memory cache.
How does JWT authentication work?
A JWT has three base64url-encoded parts separated by dots: a header that names the signing algorithm, a payload of claims, and a signature. Standard claims include sub for the subject, exp for the expiry time, iat for when it was issued and aud for the intended audience. The server signs the token with a secret key or a private key, and any service holding the matching key can verify it.
Verification checks the signature and the claims, so a service can trust the token without calling a central store. That makes JWTs popular for APIs, mobile apps and single sign-on between separate services. Remember that the payload is encoded, not encrypted, so anyone holding the token can read it. Never put passwords or other secrets in the claims.
| Aspect | Server-side session | JWT |
|---|---|---|
| Where state lives | Server store | Inside the signed token |
| Lookup per request | Yes | Not required |
| Instant logout | Delete the record | Needs short expiry or a deny list |
| Readable by the client | No, only an ID | Yes, payload is encoded |
| Typical size | Small random ID | Often several hundred bytes or more |
| Common fit | Websites on one domain | APIs and multiple services |
Where should a JWT be stored in the browser?
Storing a token in localStorage or sessionStorage makes it readable by any JavaScript running on the page. If an attacker manages a cross-site scripting injection, they can copy the token and use it elsewhere. That is why many security guides recommend keeping long-lived credentials out of script-accessible storage.
A cookie with the HttpOnly attribute cannot be read by JavaScript, the Secure attribute limits it to HTTPS, and the SameSite attribute restricts when the browser sends it with cross-site requests. MDN documents these attributes on the Set-Cookie header. You can put a JWT in such a cookie, which gives you the cookie protections while keeping the token format.
How do you log out or revoke a JWT?
A signed JWT stays valid until its exp time unless the server checks something extra. Deleting it from the browser does not stop a stolen copy from working. Common fixes include giving access tokens a short life, often minutes, and pairing them with a refresh token that the server can revoke.
Some systems keep a deny list of revoked token IDs, using the jti claim, or store a per-user version number that is checked on each request. Both bring back a server lookup, which removes part of the stateless advantage. If you find yourself checking a store on every request anyway, a plain server-side session may be simpler.
What are the main security risks of each?
Cookies are sent automatically, so cookie-based logins need protection against cross-site request forgery. SameSite=Lax or Strict blocks many cases, and anti-CSRF tokens cover state-changing requests. Session IDs must be long and random, regenerated after sign-in to prevent session fixation, and expired on the server after inactivity, as NIST and OWASP guidance describe.
JWTs bring their own pitfalls. Servers must accept only the algorithms they expect, reject unsigned tokens that use the none algorithm, validate exp, aud and iss, and protect signing keys. A token decoded without verification proves nothing, which is why a decoder is for inspection and debugging, not for trusting a token.
When should I choose a session cookie or a JWT?
Choose server-side sessions when your app is a website on one domain, when instant logout matters, or when you want the simplest secure setup. Most web frameworks support them out of the box with sensible cookie settings.
Choose JWTs when several independent services or third parties need to verify identity without sharing a session store, such as OAuth 2.0 access tokens or OpenID Connect ID tokens. Keep access tokens short-lived, store them carefully and plan revocation before you launch rather than after an incident.
How do I inspect a token with the JWT Decoder?
- Copy a test token. Use a token from a development or test environment rather than a live production credential.
- Paste and decode. Paste the compact token into the JWT Decoder and press Decode to see the header and payload JSON.
- Check the algorithm. Look at the alg value in the header and confirm it matches what your server is configured to accept.
- Read the claims. Check exp, iat, aud and sub. Convert exp from seconds since 1970 to a date to see when the token expires.
- Verify on the server. Decoding does not prove authenticity. Rely on your server’s verification library to check the signature and claims.
Example
A session cookie header
A server responds with Set-Cookie: sid=7f3c…; HttpOnly; Secure; SameSite=Lax; Path=/, and the browser returns sid on later requests to that site.
A short-lived access token
An API issues a JWT with exp set 15 minutes after iat, plus a refresh token stored in an HttpOnly cookie that the server can revoke.
Spotting a risky payload
Decoding a token reveals an email address and a role claim. That is readable by anyone holding the token, so no secrets belong there.
What should I keep in mind?
Use tested libraries
Rely on well-maintained framework session support or JWT libraries rather than writing signing and verification code yourself.
Test tokens only
Avoid pasting live production tokens into any tool. Treat a token like a password while it is valid.
Standards evolve
Check the current RFCs, NIST SP 800-63B and OWASP cheat sheets when you design authentication, since recommendations are updated over time.
FAQ
- Is a JWT more secure than a session cookie?
- Not by itself. Security depends on storage, expiry, verification and revocation. Both can be safe or unsafe depending on how they are implemented.
- Can I store a JWT in a cookie?
- Yes. Putting a JWT in an HttpOnly, Secure, SameSite cookie hides it from page scripts, though you still need CSRF protection for state-changing requests.
- Are JWTs encrypted?
- A standard signed JWT is only encoded, so its payload is readable. Encryption needs the separate JSON Web Encryption format.
- Why do JWTs use base64url instead of base64?
- Base64url swaps characters that are unsafe in URLs and drops padding, so tokens can travel in headers, cookies and query strings without escaping.
- How long should a JWT access token last?
- There is no single rule, but many systems use minutes rather than days and rely on refresh tokens to keep users signed in.
- What is a refresh token?
- A longer-lived credential used only to obtain new access tokens. The server can revoke it, which limits how long a stolen access token stays useful.
- Do session cookies work with mobile apps?
- They can, but native apps and third-party API clients often find bearer tokens in an Authorization header easier to manage than browser cookies.
- What is the jti claim used for?
- It gives a token a unique ID, which lets a server track or deny specific tokens, for example to block reuse after logout.
- Does SameSite stop all CSRF attacks?
- It blocks many, but not every case, especially across subdomains or with older browsers. Anti-CSRF tokens add a second layer for sensitive actions.
- Should I build my own session system?
- Usually not. Framework session handling and established identity providers have been tested against common attacks that custom code often misses.
Inspect a test token’s header and claims with the JWT Decoder.
Learn each part of a token in How to read a JWT.
See how token parts are encoded in What is Base64.
Create random identifiers with the UUID Generator.