DEV Community

Arhan Khan
Arhan Khan

Posted on

JWT for Beginners: How JSON Web Tokens Keep Users Logged In

After a user signs in, your server needs a way to remember them on the next request without asking for the password again. JSON Web Tokens — JWTs — are the most common answer. A JWT is a small, signed piece of data the server hands to the client, and the client sends back with every request.

What a JWT looks like

A JWT has three parts, separated by dots: header, payload, and signature.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFyaGFuIiwiaWF0IjoxNTE2MjM5MDIyfQ
.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Enter fullscreen mode Exit fullscreen mode

The header says how the token is signed. The payload carries claims — the user ID, when the token was issued, when it expires. The signature is made from the header, the payload, and a secret only the server knows. Change one character of the payload and the signature no longer matches, so the server rejects it.

How login with JWT works

  1. The user signs in with email and password.
  2. The server checks the password and creates a JWT with the user ID and a short expiry — 15 minutes is common.
  3. The client stores the token and sends it in the Authorization header on every request: Authorization: Bearer <token>.
  4. The server verifies the signature, trusts the payload, and serves the request. No database lookup needed.
// Creating a token (Node.js, jsonwebtoken library)
const jwt = require("jsonwebtoken");

const token = jwt.sign(
  { userId: user.id, role: user.role },
  process.env.JWT_SECRET,
  { expiresIn: "15m" }
);

// Verifying it on a protected route
const payload = jwt.verify(token, process.env.JWT_SECRET);
console.log(payload.userId); // trusted — the signature proves it
Enter fullscreen mode Exit fullscreen mode

Access tokens and refresh tokens

A 15-minute token alone would log users out constantly, so apps use two tokens. The access token is short-lived and sent with every request. The refresh token is long-lived, stored securely, and used only to get a new access token when the old one expires. If an access token leaks, it dies in 15 minutes. The refresh token can be revoked server-side.

Where to store tokens

  • httpOnly cookies are the safest option for web apps: JavaScript cannot read them, so cross-site scripting (XSS) cannot steal them. Pair them with SameSite and CSRF protection.
  • localStorage is convenient but risky: any script on the page — including a malicious one injected through an XSS bug — can read it.
  • Never put sensitive data in the payload. Anyone can decode a JWT — the signature only proves it was not changed, it does not hide the contents. Store a user ID, never a password or a secret.

When JWT is the wrong tool

JWTs cannot be easily revoked before they expire — the server would need a blocklist, which removes the "no database lookup" benefit. For sessions you need to kill instantly (like a logout on all devices, or a banned account), a traditional server-side session is simpler. JWT shines for APIs and services that scale across many servers.

Top comments (0)