Dashboard

What Is a JWT? How Login Tokens Work and Break

A JWT is a signed token your app hands out after login. Here is what is inside, what the signature does and does not protect, and a checklist for reviewing AI-written code.

Cecilia Iona
Cecilia Iona
Senior Editor, AI & Product
21 October 20261 min read

A JWT, short for JSON Web Token, is a small piece of text that an app gives you after you log in and that you send back with every request to prove who you are. The standard describes it as "a compact, URL-safe means of representing claims to be transferred between two parties." A claim is just a statement, such as "this user is number 123" or "this token stops working at 3pm." The server signs the token so it can later tell whether anyone tampered with it.

If an AI tool built your login, there is a good chance it uses JWTs. This guide explains what is inside one and the mistakes worth checking for.

What a JWT looks like

A JWT is three pieces separated by dots. In the words of the specification, it is "a sequence of URL-safe parts separated by period ('.') characters." In the common signed form those parts are a header, a payload and a signature.

Decoded, the first two parts are ordinary JSON. This is an illustrative example, not a real token:

header:  { "alg": "HS256", "typ": "JWT" }
payload: { "sub": "user_123", "iss": "https://example.com",
           "aud": "my-app", "exp": 1790000000 }

The header says how the token was signed. The payload carries the claims. The third part, the signature, is computed from the first two plus a secret or a private key. Change one character in the payload and the signature no longer matches, so the server rejects it.

The claims you will see most

RFC 7519 defines a short list of registered claim names. These are the ones that matter in practice:

Claim

Meaning in the specification

Why you care

iss

Identifies who issued the token

Stops tokens from another system being accepted

sub

Identifies the subject of the token

Usually the user id

aud

Identifies the recipients the token is intended for

Stops a token for one app working in another

exp

The time on or after which the token must not be accepted

Limits how long a stolen token is useful

nbf

The time before which the token must not be accepted

Rarely needed, but valid

iat

The time the token was issued

Useful for debugging

jti

A unique identifier for the token

Lets you spot replays or revoke one token

Signed does not mean secret

This is the misunderstanding that causes the most harm. A signed JWT is protected against tampering, not against reading. Anyone who holds the token can decode the payload with a few lines of code. The specification does allow encrypted tokens, but it calls support for them optional, so you should assume that what you put in the payload can be read by the person holding it.

The practical rule: never put a password, an API key or private personal data in a JWT payload. A user id and a role are fine.

A traditional session stores a random id in a cookie and keeps the real data on the server. A JWT carries the data in the token itself. Neither is automatically better.

  • JWT: the server can check a request without looking anything up, which suits several services that share one login. The cost is revocation. A signed token stays valid until exp, so logging someone out everywhere takes extra work.

  • Session cookie: easy to cancel on the server the moment you want to. The cost is a lookup on every request.

For a single small app with one server, a session is often simpler. If an AI tool chose JWTs for you and you have no reason for them, that is worth questioning.

What AI-written JWT code gets wrong

The IETF published RFC 8725, a best-practices document for JWTs, to collect the known pitfalls in JWT libraries and deployments. Use it as a checklist when you review code an agent has written:

  1. Decoding is not verifying. Many libraries have one function that reads a token and another that checks the signature. Code that only decodes accepts forged tokens. Look for the verify call.

  2. The "none" algorithm. Some libraries once trusted a token that declared its own algorithm as "none" and skipped the signature check. The document says libraries should not accept such tokens unless the caller explicitly asks.

  3. Algorithm pinning. The code should name the algorithms it accepts. The document describes an attack where a token signed with one algorithm is re-labelled to use another, tricking the server into using a public key as a secret.

  4. Weak signing secret. The document says a human-memorable password must not be used directly as the key for an HMAC algorithm such as HS256. A secret like secret123 can be guessed offline once someone has a token.

  5. Issuer and audience checks. Confirm the code validates iss and aud. Without them, a valid token from a different app may be accepted.

  6. Expiry. Look for a short exp. A token that never expires turns every leak into a permanent key.

  7. Where the token is stored. Where the browser keeps the token is a trade-off between script access and cross-site risks, so ask the agent to explain its choice.

For a deeper look at how much authority to hand an agent here, read letting an AI coding agent touch your auth code.

If you are choosing how users sign in, adding social login to an AI-built app and adding two-factor authentication show the practical steps. A token also needs a safe home, which environment variables and secrets in an AI-built app covers. For the transport underneath, see what an API is.

FAQ

Is a JWT encrypted?

Usually not. A standard signed JWT can be read by anyone who holds it. The signature only proves it was not changed. Encryption is a separate, optional layer.

How long should a JWT last?

Short enough that a stolen one does little harm. The specifications do not set a number, so decide based on how sensitive the app is, and set exp on every token.

What is the difference between a JWT and an API key?

An API key identifies an application and usually lasts until revoked. A JWT identifies a user or session and is built to expire.

Can I trust a JWT without checking the signature?

No. Decoding only reads the contents. Until the signature, issuer, audience and expiry are verified, the contents are just text someone sent you.

How did this land?

About the author

Cecilia Iona
Cecilia Iona

Senior Editor, AI & Product

Cecilia leads the Swarmz editorial desk. She has spent a decade turning complex AI and product topics into writing people actually finish, and she owns the blog's quality bar.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.