JWT Authentication Explained: How Tokens Actually Work

JWT authentication explained in plain terms: a JSON Web Token is a signed, self-contained way for a server to say “I checked this user, and here is what I know about them.” The server creates the token at login, the client stores it and sends it with each request, and the server verifies the signature instead of looking the session up in a database. That trade, no server-side session store, is why JWTs are everywhere in APIs and single-page apps. This guide shows how tokens actually work, what each part contains, and the mistakes that leak them.

JWTs are simple once you see the three parts and what each one does. The confusion usually comes from treating them as encrypted or as a session replacement, and they are neither.

The Three Parts of a Token

A JWT has three Base64URL-encoded parts, separated by dots: header, payload, and signature. The header names the signing algorithm, usually HS256 or RS256, and the token type. The payload, called claims, carries the data: the user id, the expiry time, and whatever else the server chose to include. The signature is computed over the header and payload with a secret key, and it is what makes the token trustworthy.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiIxMjM0NSIsIm5hbWUiOiJNYXlhIiwiZXhwIjoxNzYyODU0NDAwfQ
.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJVadQssw5c

Anyone can decode the first two parts. They are encoded, not encrypted, which means the payload is readable by anyone who holds the token. Never put secrets, passwords, or sensitive personal data in a payload. The signature is the only part that cannot be forged without the key, and verifying it is the entire security of the system.

That readability surprises many developers. If the payload must stay private, the answer is not a JWT alone but encryption on top, or a different mechanism entirely. For most APIs, the payload holds an id and a role, and that is enough.

How a Login Flow Works with JWTs

The flow has four steps. The client sends a username and password to the login endpoint. The server checks them, and if they match, it creates a token with the user id, a role, and a short expiry, then signs it. The client stores the token and sends it in the Authorization header of every later request, as Bearer <token>. The server verifies the signature, reads the payload, and trusts the claims.

Verification is fast because it needs no database lookup. The server checks the signature against its key, checks that the token has not expired, and proceeds. That is the main advantage over server-side sessions: any server that holds the key can verify a token, which makes horizontal scaling easy.

Storing the token is where most apps go wrong. Local storage is readable by any JavaScript on the page, so a cross-site scripting hole leaks the token. An HttpOnly cookie is readable only by the browser, not by scripts, which closes that hole, but it opens the door to cross-site request forgery, which needs its own protection. There is no storage choice without a trade-off. The guide on how to store API keys and secrets safely covers the same trade-offs for other credentials, and the threat-model guide on how to do a basic threat model for your web app shows how to list the attacks your token storage must survive.

JWT Authentication Explained: Expiry and Refresh Tokens

A token that never expires is a permanent key to the account, so tokens expire, usually after minutes. A refresh token solves the other half of the problem: it is a longer-lived token, stored more carefully, that the client exchanges for a new access token when the old one expires. The access token is short-lived and sent often, the refresh token is long-lived and sent rarely.

Refresh tokens need server-side tracking, because the server must be able to revoke them when a user logs out or a token is stolen. That tracking is a small session store, which means a real JWT system still has server state, just less of it. A design that skips refresh tokens and uses a single long-lived access token is simpler and weaker: anyone who steals the token owns the account until it expires.

Revocation is the known weak point of JWTs. A signed token stays valid until it expires, even if the user changes their password. The usual fixes are short expiry times, a denylist of revoked tokens, or a version number in the payload that the server bumps on password change. Pick one before launch, because adding revocation after users complain is harder.

Signing Algorithms: HS256 vs RS256

HS256 uses one shared secret for signing and verifying. It is simple and suits a single service that both creates and checks tokens. RS256 uses a key pair: the private key signs, the public key verifies. It suits microservices, because each service can verify tokens with the public key while only the auth service holds the private key.

The dangerous mistake is accepting tokens with the algorithm set to none, or letting an attacker switch an RS256 token to HS256 and verify it with the public key as if it were a shared secret. Modern libraries reject the none algorithm by default and require you to list allowed algorithms explicitly. Always pass that allowlist, and never let the token header choose the algorithm unchecked.

Key handling matters as much as the algorithm. A weak secret, such as a short word, can be brute-forced, and a secret committed to a repository is public. Keep signing keys in a vault or an environment variable, rotate them on a schedule, and have a plan for the transition when a key changes. The guide on the best 2FA authenticator apps in 2026 is a useful companion for the user side of auth, since tokens protect the session and a second factor protects the login itself.

When Not to Use JWTs

JWTs are a poor fit when the server needs instant revocation, when tokens carry large payloads, or when the app is a traditional server-rendered site where a session cookie already works. A session id stored in a cookie, with the data on the server, is simpler and revokes instantly. JWTs earn their complexity when many services verify tokens independently, or when the client is a mobile app or a separate frontend that talks to an API.

If you do use them, keep the rules short: short expiry, refresh tokens with revocation, minimal payloads, explicit algorithms, and keys that never reach the repository. A token system built on those rules is boring and safe, which is exactly what auth should be.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.