Course contentsShow
← Software Engineering
Lesson 1007 of 1,93331. Authentication and AuthorizationFree lesson

Session-Based Authentication Fundamentals

How servers maintain user state across requests using session identifiers stored in cookies or headers.

Session-Based Authentication Fundamentals

What you'll learn: How servers maintain user state across requests using session identifiers stored in cookies or headers.

The Problem: HTTP is Stateless

HTTP treats every request as independent—your server doesn't inherently "remember" who you are between requests. After you log in successfully with valid credentials, the next request you make would require logging in again. That's impractical.

How Session-Based Authentication Works

Think of it like a coat check at a restaurant. When you arrive (log in), you hand over your coat (credentials). The attendant verifies them, hangs up your coat, and gives you a numbered ticket (session ID). For the rest of your visit, you just show the ticket—you don't hand over your coat again.

Here's the flow:

  1. Login: User submits credentials (username + password hash comparison on server)
  2. Session Creation: Server validates credentials, creates a unique session identifier (random string like 7f3b9c2a...), and stores session data server-side (user ID, permissions, login time)
  3. Session ID Delivery: Server sends session ID to client, typically in a cookie (Set-Cookie: sessionId=7f3b9c2a...)
  4. Subsequent Requests: Client automatically includes session ID in every request (browser sends cookie)
  5. Session Validation: Server looks up session ID in its session store, retrieves associated user data, and knows who's making the request
  6. Logout/Expiration: Session is destroyed server-side, and the cookie is cleared

Where Sessions Live

Sessions are stored server-side in:

  • Memory (fast but lost on restart)
  • Database (persistent but slower)
  • Redis/Memcached (fast distributed cache)

The client only holds the session ID—never sensitive data like passwords or permissions.

Key Takeaway: Session-based authentication maintains user state by having the server store user information linked to a unique session identifier, while clients simply carry that identifier (usually in a cookie) to prove their identity across requests.