Skip to the workspace
Pebellslearn by building
Authentication Methods  ›  Lesson

Session-Based Auth

Session Based Auth 12 minIn session-based authentication the server stores the signed-in state and the browser holds only a random session id in a cookie, which makes revocation instant but needs a shared session store.

api design / Session-Based Auth

Explore each scene at your own pace.

01 · The rule

The rule

Session-Based Auth

A member signs in to the BookNook website once and browses for an hour without typing their password again. The site must recognise every later request as theirs. The classic way is a session: the server remembers the sign-in, and the browser carries a claim ticket.

The rule

The server keeps the memory; the browser keeps only a ticket.

The flow:

  1. The member posts their email and password once.
  2. The server checks them, creates a session record in its store (member id, created time, expiry), and generates a long random session id.
  3. The server sends the id in a cookie.
  4. The browser attaches the cookie to every later request. The server looks up the id, finds the session, and knows who is calling.
POST /v1/sessions HTTP/1.1
Content-Type: application/json

{"email": "asha@example.com", "password": "..."}

HTTP/1.1 201 Created
Set-Cookie: sid=kT7x2QpZr9WmVb4LcN8sEa1Y; Secure; HttpOnly; SameSite=Lax; Path=/

The state lives in the server's session store, a database or a shared cache. Strict REST prefers that servers keep no per-client session state; sessions knowingly bend that rule in exchange for simple sign-out.

Strengths:

  • Instant revocation: delete the session record and the ticket is worthless. "Sign out everywhere" is one query.
  • Nothing sensitive in the browser: the id is random and means nothing on its own.

Costs:

  • A lookup on every request, and every server in the pool must reach the same store.
  • Browser-centred: cookies suit websites; mobile apps and other servers usually prefer tokens in a header.
  • CSRF risk: because browsers send cookies automatically, another site can trigger requests carrying them. SameSite cookies and anti-CSRF tokens defend against this.

Security habits: make the id long and random, issue a new id at sign-in so an attacker cannot plant a known one beforehand, expire idle sessions, and delete the record on sign-out.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.