If you have ever logged into a website and seen a long string of characters in your browser's storage, you have probably met a JSON Web Token. It looks like gibberish, but it is three readable parts joined by dots — and once you know the structure, you can decode one by hand.
What a JWT is
A JSON Web Token, usually called a JWT and pronounced "jot," is a compact way to pass verified information between two parties. When you log in, the server creates a token containing a few facts about you — your user ID, your role, when the token expires — signs it, and hands it back. Your browser then sends that token with every request, and the server trusts it because the signature proves it has not been tampered with.
The key idea is that the token is self-contained. Unlike a session ID, which is just a random key that the server has to look up in a database, a JWT carries its own data. That makes it popular for APIs and single-page applications, where the server wants to avoid a database lookup on every request. The trade-off is that everything inside the token travels with every request, and anything in it can be read by anyone who holds it.
The three parts, separated by dots
A JWT is three Base64URL-encoded strings joined by dots: header.payload.signature. Here is a short example of the shape (truncated for readability):
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Each part decodes to something different:
- Header: says which signing algorithm was used (usually HS256 or RS256) and that this is a JWT.
- Payload: the claims — the actual data, like the user ID, the username, and the expiry time.
- Signature: a cryptographic hash of the header and payload, made with the server's secret key. It proves the token is genuine and unmodified.
Base64URL encoding is just a way to represent the JSON as safe text. It is not encryption. Anyone with the token can decode the header and payload in seconds, with no key required. That single fact drives most of the security advice about JWTs.
Reading the header and payload by hand
Decoding a JWT by hand is straightforward. Take the first part, decode it from Base64URL, and you get JSON like this:
{"alg": "HS256", "typ": "JWT"}
That header says the token was signed with HMAC-SHA256. The payload decodes to something like this:
{"sub": "1234567890", "name": "John Doe", "iat": 1516239022, "exp": 1516242622}
The sub claim is the subject — usually the user ID. The iat claim is the "issued at" time, and exp is the expiry time, both as Unix timestamps in seconds. Other common claims include iss (who issued the token), aud (who it is for), and custom claims like a user's role. A debugger tool can decode all of this in one paste, which is how developers usually inspect a token when something goes wrong with login.
How the signature works
The signature is what makes a JWT trustworthy instead of just readable. The server takes the encoded header, a dot, and the encoded payload, and runs them through the signing algorithm with a secret key only it knows. The result is the signature. When the token comes back, the server repeats the same calculation and compares: if the signatures match, the payload has not been changed since it was issued.
With HS256, the same secret key signs and verifies, so any server that needs to verify tokens must hold the secret. With RS256, the server signs with a private key and anyone can verify with the matching public key, which is why RS256 is common when many services need to check tokens from one issuer. Either way, if someone edits the payload — say, changing their role from "user" to "admin" — the signature will no longer match, and the server will reject the token.
Worked example: what changes when a token expires
Suppose a token is issued at 3:00 p.m. with an expiry of 4:00 p.m. The payload carries iat for 3:00 p.m. and exp for 4:00 p.m. — a one-hour lifetime, which is typical for access tokens. At 3:30 p.m., a request with this token is accepted: the signature checks out and the expiry is in the future. At 4:05 p.m., the same token is rejected, because the server compares the current time with exp and finds it has passed.
That is why applications use two tokens: a short-lived access token for API calls, and a longer-lived refresh token that is used only to request a new access token. If an access token leaks, it is useful to an attacker for an hour at most. If a refresh token leaks, it can be revoked on the server. Short lifetimes limit the damage of a stolen token, which is the single most effective JWT safety measure.
What never to put in a token
Because the payload is only encoded, never encrypted, anything inside it is readable by anyone who gets the token — and tokens end up in browser storage, logs, and URLs more often than anyone plans. Never put these in a JWT:
- Passwords or password hashes — they do not belong in a token, ever.
- Secrets or API keys — a token is a credential itself; it should not carry others.
- Sensitive personal data like addresses, payment details, or medical information.
- Permissions that change often — a token's claims are frozen until it expires, so a revoked role stays valid for the token's lifetime.
Keep the payload minimal: a user ID, a role, and the standard timing claims. Everything else should be looked up server-side when it is needed.
Common JWT mistakes
Three mistakes cause most JWT security problems. The first is accepting tokens with the none algorithm, which means no signature at all — a token that anyone can forge. Servers should reject alg: none outright. The second is storing tokens in localStorage, where any cross-site scripting vulnerability can read them; HTTP-only cookies are the safer home for tokens in a browser. The third is setting expiry times measured in days or weeks, which turns a stolen token into a long-term key.
Another subtle trap is trusting the payload without verifying the signature. The claims are only as reliable as the verification step — decoding a token shows you what it claims, and only the signature check proves it. Libraries handle this automatically, but hand-rolled verification code has produced some famous bypasses. Use a well-tested library, keep its version current, and let it do the checking.
Frequently asked questions
Is a JWT encrypted?
No. A JWT is encoded, not encrypted. The header and payload are Base64URL-encoded JSON, which anyone can decode with no key at all. Only the signature is cryptographic, and it proves the token has not been tampered with — it does not hide the contents. Never put secrets, passwords, or sensitive personal data inside a token.
How long should a JWT last?
Access tokens usually live 15 minutes to one hour. Short lifetimes limit the damage if a token is stolen. Applications pair a short-lived access token with a longer-lived refresh token that can request new access tokens, and the refresh token can be revoked on the server if it is compromised.
What is the difference between HS256 and RS256?
HS256 signs with a single shared secret key, so every service that verifies tokens must hold the secret. RS256 signs with a private key and verifies with a public key, so many services can check tokens without ever seeing the private key. RS256 is the better choice when several independent services need to verify tokens from one issuer.
Where should I store a JWT in the browser?
For most web applications, an HTTP-only cookie is the safer choice, because JavaScript cannot read it and cross-site scripting attacks cannot steal it. LocalStorage is convenient but exposes the token to any script running on the page. If you must use localStorage, keep token lifetimes short and make sure your site has strong XSS protections.