JWTs explained: decode one and know what to trust
What is inside a JSON Web Token, why anyone can read it, and the four checks your server must make before trusting one. With a runnable Node.js example.
A JSON Web Token is three pieces of Base64url text joined by dots: a header, a payload and a signature. The first two are just encoded JSON, so anyone holding a token can read it. Only the signature, checked with a key, tells you whether to believe it.
Every result below comes from one script, examples/jwt-explained/jwt.mjs, run on Node.js 22 with no packages installed. The raw output is in results.txt beside it.
The three parts
Here is the token the script creates:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiJ1c2VyXzEyMyIsInJvbGUiOiJlZGl0b3IiLCJpYXQiOjE3NjcyMjU2MDAsImV4cCI6MTc2NzIyOTIwMH0
.UU8cko6Z2TLpb1wSwrPysvbzd1yUovMlTI_ZX4IsihI
Paste it into the JWT decoder and you get:
{ "alg": "HS256", "typ": "JWT" }
{ "sub": "user_123", "role": "editor", "iat": 1767225600, "exp": 1767229200 }
- Header: which algorithm made the signature.
HS256is an HMAC with SHA-256 and a shared secret. - Payload: the claims.
subis who the token is about,iatwhen it was issued,expwhen it expires. The times are seconds since 1970, not milliseconds; the timestamp converter turns them into dates. - Signature: an HMAC of the first two parts, made with a secret only the server knows.
Decoding needs no key
Decoding is one line per part in Node.js:
const [h, p] = token.split('.')
const header = JSON.parse(Buffer.from(h, 'base64url').toString('utf8'))
const payload = JSON.parse(Buffer.from(p, 'base64url').toString('utf8'))
That is the whole reason not to put secrets in a payload. Email addresses, internal IDs and permissions are all readable by whoever holds the token, including a browser extension or a log file it ends up in.
Verifying is what makes it trustworthy
The script verifies with createHmac and timingSafeEqual from node:crypto. These are the results it printed:
| Test | Result |
|---|---|
Right secret, before exp | valid |
| Wrong secret | rejected: bad signature |
Payload edited from editor to admin, old signature kept | rejected: bad signature |
Right secret, checked at exp | rejected: expired |
Header changed to "alg": "none", signature empty | rejected: unexpected alg none |
The forged token still decodes to role: admin. A server that decodes without verifying would hand that user admin rights. This is the most common JWT mistake.
The four checks your server must make
- Fix the algorithm. Decide which
algyou accept and reject everything else, especiallynone. Never let the token's own header choose how it is checked. - Check the signature with your secret or public key, using a constant-time comparison.
- Check the time claims: reject if now is at or past
exp, or beforenbf. - Check who it was meant for: if you issue tokens for several services, check
aud(audience) andiss(issuer) too.
In production, use a maintained library rather than the hand-rolled check above, which exists to show what the library does. In Node.js that is usually jose or jsonwebtoken; both make you pass the allowed algorithms explicitly.
Where to keep tokens
A token is a password for as long as it is valid. Keep lifetimes short (minutes for access tokens), store them where page scripts cannot read them when you can (an HttpOnly cookie), and never paste a live one into a website that sends it to a server. The JWT decoder on this site decodes in your browser and sends nothing.