Authentication
Proving who someone is - hashed passwords, sessions and the habits that keep accounts safe.
Before this: Server-Side Validation and Errors
Objective
Implement real login: register and authenticate users with properly hashed passwords, maintain sessions via secure cookies, protect routes, and avoid the classic credential-storage mistakes.
Vocabulary
The words to use when directing your agent.
- authentication
- Verifying WHO someone is - the login half of security.
- password hash
- A one-way scramble of a password; the original cannot be recovered from it.
- salt
- Random data mixed into hashing so identical passwords produce different hashes.
- session
- Server memory of a logged-in user, referenced by a cookie the browser presents.
- httpOnly cookie
- A cookie JavaScript cannot read - stolen only via far harder attack classes.
Understand it
Generic errors are a feature
'Invalid email or password' for BOTH wrong-email and wrong-password cases. The moment your app reveals 'no such email', attackers gain a free list of registered accounts - half the work of breaking in, donated by you.
Login endpoints get special suspicion
Login is the internet's front door and favorite target: credential-stuffing replays stolen passwords millions of times. Even a simple rate limit - five attempts per minute per account - turns industrial attacks into impractical ones.
Core concepts
Hash, never encrypt, never store
Encryption is reversible with a key; hashes are not reversible at all. Passwords get hashed WITH a salt using a purpose-built slow algorithm. If you can ever show a user their password back, the design is already broken.
Sessions turn login into memory
Passwords travel once, at login. The session cookie then says 'this is user 7' on every later request. The server keeps the mapping; the cookie carries only an opaque id - httpOnly so page scripts cannot steal it.
Examples
Same password, different hashes
Register two accounts with password123 and inspect the database: two completely different stored values. That is salted hashing proving itself - a leaked hash reveals nothing about sibling accounts.
The protected route reflex
Anonymous request to /expenses gets 401 with the error envelope. Every protected route asks the session 'who is this?' before anything else - uniformly, via one middleware, not scattered ifs.
Common mistakes
Common mistake
Rolling custom crypto or 'just encoding' passwords (base64 is NOT security).
Fix
Use a standard, boring, well-tested hashing library. Cryptographic creativity is how breaches happen.
Common mistake
Trusting a userId sent in the request body or URL.
Fix
Identity comes from the VERIFIED session server-side, nowhere else. Anything else is a costume party invitation.
Working with your AI agent
Before any auth code, require your agent to answer in writing: which hashing library and parameters, where sessions live, what the cookie flags are, and what the generic error message is. Four answers reviewed up front prevent four breach classes later.
Build it
The module’s real project. Your agent builds; you direct and inspect.
Project: Login Gate
Add real authentication to the Expense API: registration, login, logout, session cookies, protected routes - with storage you would be comfortable showing a security auditor.
Requirements
- POST /register hashes passwords with a standard salted library before storage
- POST /login sets an httpOnly session cookie; GET /logout invalidates the session server-side
- All /expenses routes require a valid session: anonymous access gets 401 envelope
- Wrong-password and unknown-email return IDENTICAL generic responses
- Session expiry configured (not infinite); secret comes from environment, never committed
- Database inspection shows hashes only - zero plaintext anywhere
Context for your agent
Add authentication to my Expense API. Use a mainstream salted-password hashing library; sessions persisted server-side behind an httpOnly, secure-flagged cookie; expiry configured via env var. Protect every expense route via one auth middleware. Generic credential errors only. Before writing code, answer me in writing: library chosen and why, session storage location, cookie flags, expiry default, exact generic error message. Then implement.
Verify it - twice
Check every item yourself first. Then hand your agent the prompt and compare verdicts.
Verification prompt - paste into your AI agent
Perform a security review of my Login Gate as an application security engineer. Attempt these specifically: (1) register two accounts with identical passwords and compare stored hashes, (2) probe unknown-email vs wrong-password and diff the responses byte for byte, (3) reuse a pre-logout cookie after logout, (4) hunt for tokens in URLs and logs, (5) hammer the login endpoint past its documented limit. For each: report observed behavior, PASS or FAIL against my Definition of Done, and the fix if FAIL.
Run this against “Authentication” after your own checklist pass. Where the agent and you disagree, investigate before moving on.
Review questions
Answer out loud. If you cannot, revisit before moving on.
Explain to a friend why you could never email them their forgotten password in a well-built system.
Where does your server get 'who is asking' from on each protected request?
Why do generic login errors protect your users?
You can now
- Implement credential handling that survives an auditor's visit
- Reason about sessions, cookies and expiry as one mechanism
- Spot and refuse plaintext-password designs instantly
Up next
Authorization and Roles