JWT authentication is a method where a server issues a signed JSON Web Token after a user logs in, and the client sends that token with each request. The server verifies the signature, checks the expiry, issuer and audience, and then trusts the claims inside. A JWT is encoded, not encrypted, so it proves integrity but does not hide its contents.
Key takeaways
- A JWT has three parts — header, payload and signature — joined by dots and Base64URL encoded.
- The signature proves a token was not changed, but anyone holding it can read the payload.
- Safe JWT use means short expiry, a pinned algorithm, strong keys and full claim checks.
You log in once, and then every request you make carries proof that you are who you say you are — without the server phoning home to a database each time. That proof is usually a JWT, a JSON Web Token. It is small, portable and widely used, which is also why getting it wrong is so costly.
What is JWT authentication?
JWT authentication is a method where a server issues a signed JSON Web Token after you log in, and your client sends that token with each later request. The server checks the signature and the claims inside, then decides whether to allow the request.
A JWT is defined by RFC 7519 as a compact, URL-safe way to represent claims between two parties, as explained in the Stytch guide to RFC 7519. Claims are statements about a user or an entity, such as a user ID, a role or an expiry time. Because the token is signed, the server can trust that those claims were not changed after the token was issued.
The key idea is statelessness. With session authentication, the server stores your session and looks it up on every request. With JWT authentication, the token itself carries the information, so any server instance that knows the signing key can verify it. That makes JWTs a natural fit for APIs, mobile apps and microservices.
How does JWT authentication work?
JWT authentication follows four steps: you log in, the server issues a signed token, your client sends it with each request, and the server verifies it before responding. No database lookup is needed for verification.
- You authenticate. You send your username and password, or sign in with a provider, to the authentication server.
- The server issues a JWT. It checks your credentials, builds a payload with your user ID, roles and expiry time, signs it, and returns the token.
- Your client stores and sends it. The client keeps the token and sends it in the
Authorization: Bearer <token>header with each request. - The server verifies it. It checks the signature, the expiry time, the issuer and the audience, then serves the request if everything matches.
If any check fails, the server rejects the request. A valid signature alone is not enough: the token must also be for the right service, still within its lifetime, and signed with a key the server trusts.
What are the three parts of a JWT?
A JWT has three Base64URL-encoded parts joined by dots: header, payload and signature. Each part has a distinct job, and changing any character in the first two parts breaks the signature.
Header
The header holds metadata about how the token is protected. It names the signing algorithm in alg, for example HS256 or RS256, and usually names the token type in typ. It may also carry a kid, a key ID that tells the verifier which key signed the token.
Payload
The payload holds the claims. RFC 7519 defines registered claims such as iss (issuer), sub (subject, usually the user ID), aud (audience), exp (expiry), nbf (not valid before), iat (issued at) and jti (a unique token ID). You can add custom claims such as roles, but keep them small.
Signature
The signature is computed over the encoded header, a dot, and the encoded payload, using the algorithm named in the header. With HMAC, the same secret signs and verifies. With RSA or ECDSA, a private key signs and a public key verifies. If any part changes, verification fails.
Here is a real example from RFC 7519, shown in the Stytch guide: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c. You can paste a token into our free JWT decoder to inspect its header and payload — but only do that with test tokens, never a real one.
JWT authentication vs session authentication
Session authentication keeps state on the server and gives the client an opaque ID. JWT authentication puts the state inside a signed token, so verification needs no lookup. Each approach has real trade-offs.
| JWT authentication | Session authentication | |
|---|---|---|
| Server state | None needed to verify | Session stored on the server |
| Scaling | Easy across many servers | Needs shared session storage |
| Revocation | Hard; token lives until expiry | Easy; delete the session |
| Token size | Larger; claims travel with it | Small; just an ID |
| Confidentiality | Payload is readable | Data stays on the server |
| Best for | APIs, microservices, mobile apps | Traditional web apps with one server |
Neither is always better. A single web app with a logout button that must work at once is often simpler with sessions. An API used by many services, or a mobile app talking to several back ends, is often simpler with JWTs. Some teams use both: a JWT access token for API calls and a server-side refresh token they can revoke.
When should you use JWT authentication?
Use JWTs when several independent services must verify identity without sharing a session store. That includes public APIs, microservices, mobile apps and single sign-on, where one issuer serves many audiences.
JWTs also suit machine-to-machine authentication, where a service proves its identity to another service with a signed token. OAuth 2.0 access tokens and OpenID Connect identity tokens are commonly JWTs, which is why you meet them so often.
Do not use a JWT as a general-purpose session store. If you need instant logout, per-user revocation or large amounts of per-user data, a server-side session or a short-lived JWT plus a revocable refresh token is a better design. A token that carries everything is convenient until you need to take it back.
What are the most common JWT security mistakes?
The most common JWT mistakes are trusting the token's own algorithm, using a weak HMAC secret, skipping expiry and audience checks, and putting secrets in the payload. RFC 8725, the JWT best current practice, warns about each of these.
- Letting the token choose its algorithm. An attacker can send
alg: noneor switch RS256 to HS256 and use the public key as an HMAC secret. Pin the allowed algorithms in your code. - Weak HMAC secrets. A human-memorable password used as an HS256 key can be brute-forced offline once an attacker has one token. Use a random secret at least as long as the hash output — 256 bits for HS256 — stored in a secret manager.
- Skipping expiry. RFC 7519 says a token must not be accepted on or after its
exptime. Requireexpand keep access tokens short-lived. - Ignoring issuer and audience. Without
issandaudchecks, a token issued for one service can be replayed against another. Match both exactly. - Trusting header key pointers. Do not fetch keys from a
jkuorx5uURL named in the token. Resolve keys from the issuer's trusted key set. - Putting secrets in the payload. The payload is readable by anyone holding the token. Keep passwords, API keys and sensitive personal data out of it.
Use a maintained JWT library and never write your own verification. Libraries handle the edge cases — algorithm confusion, key types and clock skew — that hand-rolled code usually misses. The JWT attacks guide lists the same checks as a practical verification routine.
HS256 vs RS256: which signing algorithm should you choose?
Choose HS256 when the issuer and the verifier are the same service or trust domain. Choose RS256 or ES256 when separate services must verify tokens without being able to mint them.
HS256 uses one shared secret for signing and verification, so it is simple but every verifier can also create tokens. RS256 uses a private key to sign and a public key to verify, so you can publish the public key widely without risking forgery. ES256 offers similar security to RSA with smaller keys. For distributed systems, asymmetric signing is usually the safer default.
Whatever you choose, bind each key to exactly one algorithm. A key used for both HMAC and RSA verification is the classic route to algorithm-confusion attacks.
Where should you store a JWT?
Store a JWT in an HttpOnly cookie with SameSite protection for browser apps, or in memory with a refresh token flow. Avoid localStorage, where any script on the page can read the token.
Each storage choice trades one risk for another. LocalStorage is easy but exposed to cross-site scripting. Cookies are protected from scripts when HttpOnly, but need CSRF protection. Memory storage is safest from both, but the token is lost on page reload, so you need a refresh flow. Pick the option that matches your app, and always send tokens over HTTPS only.
Refresh tokens deserve the same care. Store them securely, rotate them on use, and give them a way to be revoked. A refresh token that never expires and cannot be revoked is a permanent key to the account. Check your password habits too with our password strength checker.
How do refresh tokens work with JWTs?
A refresh token is a longer-lived credential that lets a client get a new short-lived access token without asking the user to log in again. The access token does the daily work; the refresh token renews it.
The flow is simple: the client sends the refresh token to the auth server, the server checks it against its own store, and if it is valid it issues a fresh access token. Because the refresh token is checked server-side, it can be revoked at once — which solves the JWT revocation problem for the long-lived part of the session.
Rotate refresh tokens on each use and reuse detection: if an old refresh token is presented twice, revoke the whole token family. That pattern catches theft, because a stolen refresh token and the real one cannot both keep working.
The bottom line
JWT authentication is a compact, stateless way to prove identity across APIs and services — but only when the signature, algorithm, expiry, issuer and audience are all checked. A token you decode without verifying is just a claim someone typed.
Keep access tokens short, use strong random keys, pin your algorithms and never put secrets in the payload. You can inspect a test token safely with the free JWT decoder, and protect the accounts behind it with strong, unique passwords.
Frequently asked questions
What is JWT authentication?
JWT authentication is a way to prove identity with a signed JSON Web Token. After login, the server issues a token, the client sends it with each request, and the server verifies the signature instead of looking up a session in a database.
What are the three parts of a JWT?
A JWT has a header, a payload and a signature, joined by dots. The header names the algorithm and token type, the payload holds claims such as the user ID and expiry, and the signature is computed over the first two parts with a secret or private key.
Is a JWT encrypted?
No, a signed JWT is only encoded. Anyone who gets the token can decode and read the payload, so you should never put passwords, secrets or sensitive personal data in it. If the contents must stay private, use an encrypted token or keep the data on the server.
How long should a JWT last?
Access tokens should be short-lived, often 5 to 60 minutes, with a separate refresh token for longer sessions. A short lifetime limits the damage if a token is stolen, because the token stays valid until it expires.
What is the difference between JWT authentication and session authentication?
Session authentication stores state on the server and gives the client an opaque session ID. JWT authentication puts the claims inside the token itself, so any server that knows the signing key can verify it without a database lookup, which suits APIs and microservices.
What does alg none mean in a JWT?
Alg none means the token has no signature. It was defined for unsecured tokens, but it should never be accepted in production. A verifier must use an explicit list of allowed algorithms and reject none.
Can a JWT be revoked?
A JWT cannot be cancelled on its own, because verification needs no server state. Teams handle this with short expiry, refresh token rotation, a deny list for stolen tokens, or sender-constrained tokens, and by rotating signing keys when needed.
Should I store a JWT in localStorage?
Storing a JWT in localStorage makes it readable by any script on the page, so cross-site scripting can steal it. A safer choice for browser apps is an HttpOnly cookie with SameSite protection, or keeping the token in memory and using a refresh token flow.
Sources
- Stytch — A developer's guide to RFC 7519, part 1: JWT structure and claims
- Azion — What is JWT (JSON Web Token)? Stateless Authentication Explained
- DEV — The Definitive Guide to JWT Tokens
- Software Security — JWT attacks: prevention and correct verification
- Medium — The Ultimate Guide to JWT Architecture: Structure, Verification, and Common Flaws
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 7519, RFC 8725 best current practices and the OWASP JSON Web Token guidance summarized in the sources.
How we write and fact-check →