API keys identify applications, but products with human users need per-user sessions that carry identity, expire automatically, and scale across servers. JSON Web Tokens (JWTs) are the dominant answer for REST APIs. In this lesson we dissect the token format, build the login flow, and cover the refresh-token pattern and the pitfalls that regularly cause real-world breaches.
A JWT is three base64url-encoded parts joined by dots: header.payload.signature.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 <- header: {"alg":"HS256","typ":"JWT"}
.eyJzdWIiOiJ1XzQyIiwicm9sZSI6ImVkaXRvciIsImV4cCI6MTc4NDAwMDAwMH0
.p3k1Zx9v... <- signatureThe payload holds claims: standard ones like sub (subject, the user ID), exp (expiry timestamp), iat (issued at), and custom ones like role. The signature is computed over the first two parts with a secret key (HS256) or a private key (RS256). Anyone can decode and read a JWT; only holders of the key can create a valid signature. JWTs provide integrity and authenticity, not secrecy, so never put passwords or sensitive data in the payload.
Authorization: Bearer <token>.Statelessness has a dark side: a signed token stays valid until exp, and you cannot easily un-sign it. The standard mitigation is a pair of tokens. The access token is short-lived, around 15 minutes, and is what accompanies API calls. The refresh token is long-lived, days or weeks, stored server-side in a database so it can be revoked, and is used only against /auth/refresh to mint new access tokens.
When a user logs out or is compromised, you revoke the refresh token; the attacker's window shrinks to whatever remains of the 15-minute access token. Rotating refresh tokens on each use, and invalidating the family if an old one is replayed, closes the window further.
For browser clients, localStorage is readable by any script, so one XSS vulnerability leaks every user's token. The safer pattern is an httpOnly, Secure, SameSite cookie for the refresh token, with the access token held only in memory. Mobile and server-side clients can use their platform's secure storage. Whatever you choose, transmit tokens exclusively over HTTPS.
Next up: API Versioning Strategies, evolving your API without breaking its clients.