Step 23 of 29
JWT/sessions, OAuth concepts, input validation, common vulnerabilities (OWASP)
AuthN vs AuthZ, sessions vs JWT — เข้าใจ concept ก่อน แล้วค่อย implement ใน stack นี้
Authentication answers "who are you?" Authorization answers "what can you do?" They are different things. Confusing them causes security holes.
Loading diagram...
Pros: Server can revoke sessions instantly. Session data stays server-side. Cons: Hard to scale across multiple servers (need shared session store). Stateful.
Pros: Stateless. Easy to scale. Works across services. Cons: Cannot revoke instantly (token is valid until expiry). Token contains data that can get stale.
A JWT has three parts separated by dots: header.payload.signature
xxxxx.yyyyy.zzzzz
Header — Algorithm and token type:
{
"alg": "HS256",
"typ": "JWT"
}
Payload — Claims (data). Do not put secrets here:
{
"sub": "user-42",
"name": "Alice",
"role": "admin",
"exp": 1700000000
}
Signature — Proves the token was not tampered with:
HMACSHA256(base64(header) + "." + base64(payload), secret_key)
JWT is:
JWT is not:
| Need | Use |
|---|---|
| Instant revocation | Server-side sessions |
| Scale across services | JWT |
| Simple app, one server | Sessions |
| Microservices / API | JWT |
| Both | JWT + refresh tokens with a revocation list |
iss (issuer) and aud (audience).