๐Ÿ”‘ JWT: JSON Web Tokens Explained

How stateless authentication works โ€” from structure to security

What Is JWT?

JWT (JSON Web Token) is a compact, self-contained token format for securely transmitting information between parties. It consists of three Base64 URL-encoded parts separated by dots: header.payload.signature.

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
In a sentence: JWT is a signed JSON object. The server issues it, the client stores it, and the server verifies the signature to trust it โ€” no database lookup needed.

Three Parts of JWT

1. Header

{"alg": "HS256", "typ": "JWT"}

2. Payload

Contains claims (data). Standard fields:

FieldMeaning
subSubject (user ID)
issIssuer
expExpiration (Unix timestamp)
iatIssued at

โš  Never put secrets in payload โ€” it's only Base64-encoded, not encrypted. Anyone can decode it.

3. Signature

Signature = HMAC-SHA256(header + "." + payload, secret). This proves the token wasn't tampered with.

JWT vs Session Auth

DimensionJWTSession
StorageClient-sideServer-side (Redis/DB)
Scalabilityโœ… Stateless, microservice-friendlyโš  Needs shared session store
Logoutโš  Hard to invalidate immediatelyโœ… Delete session

Security Best Practices

๐Ÿ‘‰ Decode a JWT now

Header/Payload visualization, expiry check, client-side decoding

๐Ÿ”‘ Open JWT Decoder

FAQ

Q: Is JWT encrypted?

No โ€” JWT is signed, not encrypted. Anyone can decode the payload. The signature only proves authenticity, not confidentiality. For encryption, use JWE.

Q: How to handle logout with JWT?

Maintain a token blacklist (Redis), or use short-lived access tokens with refresh tokens so logout is effectively handled by expiration.

๐Ÿ“– Related: SHA256 Hash โ†’ ยท RSA Encryption โ†’ ยท Base64 Encoder โ†’