2 MINAUTH, SECURITY, API, WEB

JWTs vs traditional login sessions

Verdict

JWTs are stateless, secure and light on the server: each request carries its own signed proof, with nothing to store or look up.

When I'd pick the other one

A traditional server-rendered app on one backend, or a regulated system, like banking or healthcare, that must end a session the instant it is flagged.

A session keeps a list of who’s logged in, and every request looks you up in it. A JSON Web Token (JWT) carries its own proof: a signed note saying who you are and when it expires, which any service with the key can check without asking anyone. When one API serves a web app, a mobile app and other services across regions, nothing has to share a session store.

JWT
Log in The server signs a token saying who you are and when it expires.
Each request Any server with the key checks the signature. Nothing is looked up.
Log out The token works until it expires, so it's kept to minutes. The refresh token is revoked.
Session
Log in The server stores the session and sends your browser its ID in a cookie.
Each request The server looks the ID up in its session store.
Log out The server deletes the entry. It takes effect everywhere at once.

Logging someone out still works. Access tokens last minutes, and the refresh token that renews them can be revoked, so a logout takes effect within one token lifetime. Keep tokens in memory or an HttpOnly cookie, never in local storage, where any script on the page can read them.

Tokens are safer when the session store gets breached. It holds a live credential for every logged-in user, unless the IDs are hashed. JWTs have no such list to steal, and with a key pair, services that check tokens can’t issue them. A token sent in a header also can’t be forged across sites, because the browser never attaches it on its own.

Sessions are safer when access has to end right now. Delete a session and the next request fails. A stolen token keeps working until it expires, and a demoted admin stays an admin until the next refresh. For moving money or reading medical records, that window is too long.

Outside those cases, I’d use tokens. This site’s authentication demo uses tokens, so you can see inside one.

Tyler SandersEngineer, Computer Scientist, Tech Enthusiast