Auth Flow Overview
Modern authentication is stateless: the server issues a signed token and never stores session state. Every subsequent request carries the token, and the server verifies it cryptographically — no database lookup required.
Session vs Token
Sessions store state on the server; tokens encode state in the client. Each approach has clear trade-offs depending on your scale and architecture.
| Session-based | Token-based (JWT) |
|---|---|
| State stored on server | State encoded in token |
| Easy to revoke (delete session) | Hard to revoke before expiry |
| Requires sticky sessions or shared store | Stateless — scales horizontally |
| Cookie with session ID sent automatically | Token sent manually in Authorization header |
| Best for monoliths and server-rendered apps | Best for APIs, SPAs, and microservices |
JWT Anatomy
A JWT is three Base64URL-encoded JSON objects joined by dots. The signature ties the header and payload together — if any bit changes, the signature check fails and the token is rejected.
// JWT = three Base64URL parts joined by dots
// eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyMTIzIn0.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
// Header — signing algorithm and token type
{ "alg": "HS256", "typ": "JWT" }
// Payload — claims: user data and expiry
{ "sub": "user123", "role": "admin", "exp": 1700000000 }
// Signature — proves the token was not tampered with
HMACSHA256(base64url(header) + "." + base64url(payload), secret)OAuth 2.0 Flow
OAuth 2.0 lets users grant your app limited access to a third-party service without sharing passwords. The authorization code flow is the standard for web apps.
Security Best Practices
httpOnly, Secure, SameSite=Strict cookies so the token is invisible to scripts and sent only over HTTPS.