Learn how JWT works internally by understanding its structure: header, payload, and signature with real-world examples and code.
JSON Web Tokens (JWT) are one of the most important concepts in modern authentication systems. But many developers use JWT without fully understanding how it actually works internally.
If you truly want to master JWT, you must understand its structure:
In this guide, we will break down each part in detail with real examples and code.
๐ You can instantly decode and inspect JWT tokens here: https://www.mydevtoolhub.com/tools/jwt-decoder
A JWT token looks like this:
xxxxx.yyyyy.zzzzz
It consists of three parts separated by dots:
Header.Payload.Signature
Each part is Base64URL encoded.
The header contains metadata about the token.
{
"alg": "HS256",
"typ": "JWT"
}
alg โ Algorithm used for signing (e.g., HS256, RS256)typ โ Token type (JWT)eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
The payload contains the actual data (claims).
{
"userId": "123",
"email": "user@example.com",
"role": "admin",
"exp": 1710000000
}
iss (issuer)sub (subject)exp (expiration)iat (issued at)โ ๏ธ Payload is NOT encrypted โ anyone can decode it.
The signature ensures the token has not been tampered with.
HMACSHA256(
base64UrlEncode(header) + "." + base64UrlEncode(payload),
secret
)
const jwt = require('jsonwebtoken');
const token = jwt.sign({ userId: 123 }, 'secret', {
algorithm: 'HS256'
});
HEADER PAYLOAD SIGNATURE
----- ------- ---------
eyJhbGci... eyJ1c2Vy... SflKxwR...
{ "alg": "HS256", "typ": "JWT" }
{ "userId": "123" }
header.payload.signature
You can manually decode JWT:
const base64 = token.split('.')[1];
const decoded = JSON.parse(Buffer.from(base64, 'base64').toString());
console.log(decoded);
Or use: https://www.mydevtoolhub.com/tools/jwt-decoder
It is only encoded.
Never store passwords or secrets.
Always verify JWT before trusting it.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.
eyJ1c2VySWQiOiIxMjMiLCJleHAiOjE3MDAwMDAwMDB9
.
abc123signature
{ "alg": "HS256", "typ": "JWT" }
{ "userId": "123", "exp": 1700000000 }
| Algorithm | Type | Key |
|---|---|---|
| HS256 | Symmetric | Same secret |
| RS256 | Asymmetric | Public/Private |
Yes, but signature will break.
To separate metadata, data, and verification.
Yes, for secure JWT.
Only after verification.
Understanding JWT structure is the foundation of secure authentication.
Once you know how header, payload, and signature work, debugging and securing your app becomes much easier.
๐ Try decoding your own token here: https://www.mydevtoolhub.com/tools/jwt-decoder
Master the structure โ and you master JWT.
Master URL encoding with real-world examples including forms, search queries, APIs, and redirects. A practical guide for developers.
Learn how URL encoding protects your web apps from XSS and SQL injection attacks. A practical security guide for developers.
Learn how to implement URL encoding in JavaScript, Node.js, and Python with real-world examples, best practices, and developer-focused insights.