Understanding JWT: How Secure Token Authentication Works
What is a JWT?
JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. In plain English, it's a digital pass card that proves who you are.
Unlike session cookies which require the server to remember you (stateful), JWTs are stateless. The token itself contains all the necessary info.
The Anatomy of a JWT
A JWT has three parts joined by dots (.):
Header.Payload.Signature
- Header: Describes how the token is signed (e.g.,
HS256algorithm). - Payload: The data! User ID, name, admin status, and expiration (
exp). - Signature: The security seal. It's calculated using the Header, Payload, and a secret server key.
Visualise it: Paste any token into our JWT Debugger to see these three parts decoded instantly.
Security Best Practices
1. Don't Store Secrets
Never store passwords or sensitive PII in the Payload. The Payload is Base64 encoded, not encrypted—anyone who has the token can read it.
2. HTTPS is Mandatory
Since the token gives access to an account, sending it over HTTP (unencrypted) allows attackers to steal it. Always use HTTPS.
3. Short Expiration
Tokens should expire quickly (e.g., 15 minutes). Use Refresh Tokens to get new access tokens. This limits damage if a token is stolen.
4. Storage: HttpOnly Cookies vs LocalStorage
- LocalStorage: Easy to use, but vulnerable to XSS attacks (malicious JS reading your token).
- HttpOnly Cookies: More secure against XSS, but requires CSRF protection.
Conclusion
JWTs are the backbone of modern authentication. Understanding their structure helps you build safer apps. Use the JWT Debugger whenever you're stuck with "401 Unauthorized" errors!