Tutorial

Stateless Session with JWT

Put the session in a signed JWT so the server keeps no session table, enforce expiry and claims, and accept the trade-off that revocation needs a list.

On this page

  1. Introduction
  2. Carry the claims
  3. Sign and expire
  4. Verify on every request
  5. Revocation list
  6. Keep it small
  7. Test expiry and revoke
  8. Sources

Introduction

This guide covers a stateless session backed by JWT, a recurring concern in the auth-jwt scenario. A server-side session table is a lookup on every request and a shared piece of state to invalidate. A signed JWT carries the session in the token, so the server verifies a signature instead of hitting a store, which is faster and scales horizontally, at the cost of making revocation explicit.

Carry the claims

Put the user id and role in the payload so a request carries its own authorization, and the server never reads a session store to know who is calling.

The trade-off worth naming is that put the user id and role in the payload so a request carries its own authorization, and the server never reads a session store to know who is calling adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

const jwt = await new SignJWT({ sub: user.id, role: user.role }).setProtectedHeader({ alg: 'HS256' }).setExpirationTime('15m').sign(secret);

Sign and expire

Sign with a key only the server holds and set a short expiry, so a leaked token is only useful until it expires and cannot be forged without the key.

The trade-off worth naming is that sign with a key only the server holds and set a short expiry, so a leaked token is only useful until it expires and cannot be forged without the key adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Verify on every request

Check the signature and expiry in middleware so an unsigned or expired token is rejected before the handler runs, which is the whole point of stateless.

The trade-off worth naming is that check the signature and expiry in middleware so an unsigned or expired token is rejected before the handler runs, which is the whole point of stateless adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

async function auth(req, res, next) {
  const t = req.cookies.jwt;
  if (!t) return res.status(401).end();
  try { req.user = await jwtVerify(t, secret); next(); }
  catch { res.status(401).end(); }
}

Revocation list

Keep a short list of revoked jti values for forced logout, since stateless cannot recall a token, and check it on verify; the list is bounded by the expiry window.

The trade-off worth naming is that keep a short list of revoked jti values for forced logout, since stateless cannot recall a token, and check it on verify; the list is bounded by the expiry window adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Keep it small

Do not put large or sensitive data in the token, since it is base64 in a cookie and visible to the client; keep it to ids and roles and look up the rest if needed.

The trade-off worth naming is that do not put large or sensitive data in the token, since it is base64 in a cookie and visible to the client; keep it to ids and roles and look up the rest if needed adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test expiry and revoke

Assert an expired token is rejected, a revoked jti is rejected, and a valid token yields the claims, so the stateless contract is pinned.

The trade-off worth naming is that assert an expired token is rejected, a revoked jti is rejected, and a valid token yields the claims, so the stateless contract is pinned adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: JWT RFC 7519, Stateless auth.

FAQ

How do I log someone out before their token expires?
You cannot do it purely statelessly; keep a short revocation list of jti values and check it on verify, which is a small, bounded store rather than a full session table.
What goes in the claims?
The user id, role, and an expiry; avoid putting anything sensitive or large in the token, since it is base64 in a cookie and visible to the client.

Related Examples