Course contentsShow
← Web Development
Lesson 1569 of 2,98229. Web Authentication PatternsFree lesson

Token-Based Authentication Flow

How access tokens are issued, stored client-side, and sent with each request for stateless authentication.

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:

  1. User logs in — client sends credentials (username/password) to the server
  2. Server validates and issues an access token (often a JWT)
  3. Client stores the token — typically in localStorage, sessionStorage, or memory
  4. Every subsequent request includes the token in the Authorization header:
    Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
    
  5. 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 attacks
  • sessionStorage: 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.