When an API call fails with a 401, one of the first things to check is the token. Is it expired? Does it have the right audience, scopes or user ID? A JWT carries all of that in plain sight. You just have to decode it.
You don't need a library for that. Here's how a JWT is put together, a small decoder you can paste into the console, and the one mistake that turns this into a security bug.
What's inside a JWT
A JWT is three parts separated by dots:
xxxxx.yyyyy.zzzzz header.payload.signature - Header: JSON describing the token, like
{"alg":"HS256","typ":"JWT"}. - Payload: JSON with the claims, like
sub,exp,iat,aud, plus whatever your app adds. - Signature: proof that the header and payload weren't changed, created with a secret or private key.
The header and payload are not encrypted. They're just Base64URL-encoded JSON. Anyone holding the token can read them.
Base64URL is not quite Base64
This is where most hand-written decoders break. Base64URL is Base64 with two changes so it's safe in URLs:
-
+becomes-and/becomes_ - the
=padding at the end is removed
So before you can use atob(), you have to undo both.
The decoder
function decodeJwtPart(part) { // Base64URL -> Base64 let b64 = part.replace(/-/g, '+').replace(/_/g, '/'); // restore padding b64 += '='.repeat((4 - (b64.length % 4)) % 4); // atob gives a "binary string"; turn it into UTF-8 text const bytes = Uint8Array.from(atob(b64), (c) => c.charCodeAt(0)); return JSON.parse(new TextDecoder().decode(bytes)); } function decodeJwt(token) { const [header, payload, signature] = token.split('.'); if (!signature) throw new Error('Not a JWT: expected three parts'); return { header: decodeJwtPart(header), payload: decodeJwtPart(payload), }; } The TextDecoder step matters. atob() returns one character per byte, so a payload containing a name like "José" or any non-Latin text comes out garbled if you JSON.parse(atob(...)) directly. Decoding the bytes as UTF-8 fixes that.
Reading the time claims
exp, iat and nbf are seconds since the Unix epoch, not milliseconds. That trips people up constantly:
const { payload } = decodeJwt(token); const expiresAt = new Date(payload.exp * 1000); const isExpired = Date.now() >= payload.exp * 1000; console.log(expiresAt.toISOString(), isExpired ? 'expired' : 'still valid'); Also check the machine's clock. If a server's clock is a few minutes off, freshly issued tokens can look "not yet valid" or already expired. Many verifiers allow a small leeway for this.
The important part: decoding is not verifying
Decoding only reads the token. It does not prove the token is genuine. Anyone can create a header and payload that say "role": "admin", Base64URL-encode them, and attach a junk signature. Your decoder will happily show those claims.
So:
- On the client, decoding is fine for display and convenience, like showing the user's name or knowing when to refresh a token. Never use it to decide what someone is allowed to do.
- On the server, always verify the signature with a proper library before trusting any claim, and pin the algorithms you accept. Don't let the token's own
algheader choose for you.
Be careful where you paste tokens
A valid access token is a live credential until it expires. Pasting a production token into a website that sends it to a server means handing that credential to someone else.
That's why I built SnipFerry's JWT decoder to run entirely in the browser tab. It decodes the header and payload, shows whether the token is expired or not yet valid, and can verify HS256/384/512 signatures with your secret, without sending the token anywhere.
What's the strangest thing you've found inside a JWT while debugging?