Tutorial

JWT Claims Design

Choose the right claims for a JWT, use registered names like sub and exp, add custom claims for authorization, and keep the payload small and non-sensitive.

On this page

  1. Introduction
  2. Use registered names
  3. Custom for authorization
  4. Keep it small
  5. No personal data
  6. jti for revocation
  7. Test authorization
  8. Sources

Introduction

This guide covers designing JWT claims, a recurring concern in the auth-jwt scenario. The JWT payload is base64 in a cookie or header, so it is visible to the client and counts toward request size. Picking the right claims, using registered names where they apply, and keeping the payload to ids and roles rather than personal data makes the token small, standard, and safe to expose.

Use registered names

Use sub for the subject, exp for expiry, iat for issued-at, and jti for the id, so verifiers and libraries know the fields without custom mapping.

The trade-off worth naming is that use sub for the subject, exp for expiry, iat for issued-at, and jti for the id, so verifiers and libraries know the fields without custom mapping 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, tid: tenant.id })
  .setProtectedHeader({ alg: 'HS256' })
  .setIssuedAt().setExpirationTime('15m').setJti(crypto.randomUUID()).sign(secret);

Custom for authorization

Add role and tenant as custom claims so the API can authorize from the token without a lookup, which is the stateless payoff.

The trade-off worth naming is that add role and tenant as custom claims so the api can authorize from the token without a lookup, which is the stateless payoff 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

The token is base64 on every request, so keep the payload to ids and roles; a fat token bloats every call and leaks structure to the client.

The trade-off worth naming is that the token is base64 on every request, so keep the payload to ids and roles; a fat token bloats every call and leaks structure to the client 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.

No personal data

Do not put email, name, or address in the token, since it is client-visible and personal data; carry an id and look up the rest in the handler if needed.

The trade-off worth naming is that do not put email, name, or address in the token, since it is client-visible and personal data; carry an id and look up the rest in the handler 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.

// bad: { sub: id, email, name }
// good: { sub: id, role } — look up email server-side
const user = await db.query('SELECT email FROM users WHERE id=$1', [claims.sub]);

jti for revocation

Set a jti on every token so you can revoke a single one before expiry and detect replay, which is impossible without a unique id.

The trade-off worth naming is that set a jti on every token so you can revoke a single one before expiry and detect replay, which is impossible without a unique id 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 authorization

Assert a token with role admin passes an admin check and role user fails, so the claims actually drive authorization rather than being decorative.

The trade-off worth naming is that assert a token with role admin passes an admin check and role user fails, so the claims actually drive authorization rather than being decorative 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, IANA JWT claims.

FAQ

Should I put the user email in the token?
Only if you need it on every request and it is not sensitive; email is personal data and the token is client-visible, so prefer a user id and look up the email when needed.
What is jti for?
It is a unique token id used for revocation and replay detection; without it you cannot revoke a single token before its expiry.

Related Examples