JWT (JSON Web Token) Guide — Structure, Validation & Best Practices

Understand JWTs: header, payload, signature. Claims, expiration, refresh tokens, security best practices.

JWT Structure

A JWT has 3 parts separated by dots: header.payload.signature

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.   ← Header (Base64)
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6Ik   ← Payload (Base64)
pvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.    ← Signature
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Header

{
  "alg": "HS256",  // Algorithm: HS256, RS256, ES256
  "typ": "JWT"
}

Common Claims (Payload)

ClaimNameDescription
subSubjectUser ID
issIssuerWho issued the token
audAudienceIntended recipient
expExpirationUnix timestamp expiry
iatIssued AtWhen token was created
nbfNot BeforeToken not valid before
jtiJWT IDUnique token identifier

Security Best Practices

Node.js Example

import jwt from 'jsonwebtoken';

// Create token
const token = jwt.sign(
  { userId: 123, role: 'admin' },
  process.env.JWT_SECRET,
  { expiresIn: '15m' }
);

// Verify token
try {
  const decoded = jwt.verify(token, process.env.JWT_SECRET);
  console.log(decoded.userId); // 123
} catch (err) {
  console.error('Invalid token');
}

Need These Tools as an API?

TextForge API offers 20+ developer toolkit endpoints. Free tier: 50 requests/day.

Try TextForge API Free →

Related Tools