Authentication patterns
Sessions vs JWTs (revocation, rotation, storage), OAuth2 flows (auth code + PKCE), OIDC, password hashing with bcrypt/argon2. Stateless is a tradeoff, not a default.
The mental model
Authentication answers one question: who are you? (Authorization, a separate topic, answers what may you do?) The whole discipline is about issuing a credential after a user proves their identity once, then accepting that credential on every subsequent request without re-checking the password each time.
Two mental models, and the entire field is a choice between them:
- Sessions are a coat check. You hand over your coat (log in), get a numbered ticket (session id — an opaque random string), and the cloakroom keeps your coat. The ticket means nothing to anyone else; all the information lives server-side, looked up by the ticket. To revoke you, the cloakroom tears up its record and your ticket is now worthless.
- JWTs are a signed festival wristband. At the gate you show ID once and get a wristband that *itself* says "adult, VIP area, valid until 6pm", stamped with a seal nobody can forge. Guards inside never call the gate — they just check the seal and read the band. Fast and stateless, but here is the catch: once the band is on your wrist, nobody can take it off until 6pm. You cannot revoke a wristband; you can only wait for it to expire.
That single difference — server-side lookup versus self-contained token — drives every real decision. Sessions are trivially revocable but require a lookup (state) on every request. JWTs are stateless and scale beautifully but are painful to revoke, which is why the correct JWT design uses short lifetimes and a separate refresh mechanism.
Why any of this exists: without a credential you would send the password on every request, storing it in the browser and transmitting it constantly — a catastrophe the first time any log, proxy, or memory dump is examined. The session or token is the thing that lets a user prove identity once and then not carry the password around. And the reason password hashing is non-negotiable is that databases leak: the question is never "if" your user table is exposed but "when", and the only thing standing between a leak and mass account takeover is that the stored values are slow-to-crack hashes, not passwords.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision