At a music festival, you show your ticket once at the entrance. The staff check it against the list and put a stamped wristband on you. After that, every guard inside just glances at the wristband. They don’t call the ticket office each time. The stamp is hard to fake, so the wristband itself is the proof.
A JWT works the same way for websites and APIs. You sign in once, and the server gives your app a signed token. After that, the app shows the token with every request, and any server can check it on the spot. This guide explains what is inside a JWT, how the signature works, and how to use JWTs safely.
What is a JWT?
JWT stands for JSON Web Token (often said “jot”). It is a compact, signed piece of text that carries claims: facts about the user, like “this is user 42” and “this token expires at 10:30”.
A JWT looks like a long string with two dots in it:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0MiIsIm5hbWUiOiJBc2hhIiwiZXhwIjoxNzk1MDAwMDAwfQ.k3jF8…
Those dots split it into three parts.
- Header: which signing method is used, for example
{"alg": "HS256", "typ": "JWT"}. - Payload: the claims. Common ones are
sub(subject, the user ID),exp(expiry time),iat(issued at), plus your own, likerole. - Signature: a cryptographic stamp made from the header and payload using a key that only the server (or the sign-in service) holds.
The header and payload are written in Base64URL, a way of turning text into URL-safe characters. That’s encoding, not encryption: anyone who has the token can decode and read them in seconds.
How the signature makes it trustworthy
When the server creates the token, it computes the signature from the header, the payload and its secret key. When a request comes back with the token, the server recomputes the signature and compares.
- If someone changed the payload (say,
"role": "customer"to"role": "admin"), the signature no longer matches, and the token is rejected. - Without the secret key, nobody can produce a valid new signature.
There are two families of signing:
- Shared secret (HMAC, like HS256): the same key signs and checks. Simple, but every service that checks tokens must hold the secret.
- Public/private key (like RS256 or ES256): a private key signs, and a public key checks. The sign-in service keeps the private key; any service can verify with the public one, which is safe to share.
How JWT sign-in works
- You send your username and password (or sign in with Google) to the sign-in service.
- It checks them and returns a JWT, valid for a short time, say 15 minutes.
- Your app sends the token with each request, usually in a header:
Authorization: Bearer <token>. - Any server that receives the request checks the signature and the expiry, reads
subandrole, and decides what you may do. No database lookup is needed.
That last point is the big win in system design. Because the token carries everything needed, any copy of any service can check it without asking a central session store. That makes JWTs popular with API gateways and microservices.
JWT vs session cookie
The older approach is a session: the server stores “session 7f3a belongs to user 42” in its memory or a database, and the browser holds only the random ID 7f3a in a cookie.
| Session ID | JWT | |
|---|---|---|
| What the client holds | A random ID | A signed token with the claims inside |
| Where the facts live | On the server (database or cache) | In the token itself |
| Checking a request | Look up the ID | Check the signature, no lookup |
| Logging someone out right now | Delete the session, done | Hard: the token stays valid until it expires |
| Good fit | Classic web apps on one domain | APIs, mobile apps, many services |
Neither is “better”. Many systems combine them. Our guide to security in system design compares them in more depth.
The catch: you can’t easily take a JWT back
Because servers don’t look anything up, a stolen or no-longer-wanted token keeps working until its exp time. If a user is banned at 10:00 and their token expires at 18:00, they have eight more hours.
The usual answer is two tokens:
- A short-lived access token (a JWT, valid for minutes) sent with every request.
- A longer-lived refresh token, stored more carefully and checked against a database, used only to get a new access token.
Ban a user, and their refresh token is refused; within minutes their access token expires too. For urgent cases, some systems also keep a small deny list of revoked token IDs.
Common misconceptions
“A JWT is encrypted, so the data is hidden.” A normal JWT is signed, not encrypted. Anyone can read the payload. (There is a separate encrypted format, JWE, but it’s less common.)
“JWT is a sign-in system.” JWT is just a token format. Sign-in systems like OAuth 2.0 and OpenID Connect often use JWTs, but they’re different things.
“JWTs are always better than sessions.” They save lookups but make instant logout harder. Pick based on your needs.
Key takeaways
- A JWT is a signed token with three parts: header, payload, signature.
- The payload is readable by anyone; the signature stops tampering.
- Servers can verify a JWT without a database lookup, which suits APIs and microservices.
- JWTs are hard to revoke, so keep access tokens short and use refresh tokens.
- Never put secrets in the payload, always verify the signature and expiry, and use HTTPS.
Frequently asked questions
What is a JWT in simple words?
A JWT is a signed digital pass. After you sign in, the server gives your app this pass, and the app shows it with every request so the server knows who you are without asking you to sign in again.
What are the three parts of a JWT?
The header (which signing method is used), the payload (the claims, like user ID and expiry time) and the signature (a stamp that proves the first two parts weren’t changed).
Is a JWT encrypted?
Usually not. A standard JWT is signed, which prevents changes, but its contents can be decoded and read by anyone. Don’t put secrets in it.
What is the difference between a JWT and a session?
With a session, the server stores your sign-in state and gives you only an ID to look it up. With a JWT, the sign-in facts are inside the signed token, so the server can trust it without a lookup.
How do you log out a user with JWT?
Delete the token on the client, keep access tokens short-lived so they expire quickly, and revoke the refresh token on the server. For urgent cases, keep a deny list of revoked token IDs until they expire.
Comments