Token-Based Authentication Flow
You'll learn: How servers issue access tokens after login, how clients store and send them, and why this enables stateless authentication.
The Core Concept
Token-based authentication trades credentials for a token — a cryptographic string that proves "I already logged in successfully." Unlike cookie-based auth where the server maintains session state, token-based flows are stateless: the server doesn't remember you between requests. Instead, you prove your identity by presenting the token with each request.
The Flow:
- User logs in — client sends credentials (username/password) to the server
- Server validates and issues an access token (often a JWT)
- Client stores the token — typically in
localStorage,sessionStorage, or memory - Every subsequent request includes the token in the
Authorizationheader:Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... - Server validates the token on each request without checking a session store
Why Stateless?
The token itself contains encoded information (like user ID and expiration). The server verifies the token's signature to confirm it wasn't tampered with, then extracts the user info — no database lookup needed. This makes scaling easier since any server can validate any token.
Storage Trade-offs
localStorage: Persists across browser restarts but vulnerable to XSS attackssessionStorage: Cleared when tab closes, slightly more secure- Memory: Most secure but lost on page refresh (requires refresh token pattern)
Key Takeaway: Token-based authentication issues a cryptographic proof of login that clients store locally and send with every request via the Authorization header, enabling stateless server verification without session storage.