Building a login flow
Understand the idea
Register stores a hash. Login checks it. The cookie is HttpOnly. The route asks for that session.
The idea
Registration stores a salt and a scrypt hash, never the password. Login recomputes the hash and compares it with a timing-safe equal. On success the response names the cookie the browser should store: HttpOnly so script cannot read it, Secure so it rides only on HTTPS, SameSite=Lax so a cross-site POST does not include it. Each example starts from the seeded book, so the notes request looks up sid-ada the way the next request would look up the cookie value. middleware runs before the notes handler. No row means 401, and the handler does not run.
What changes
| Step | Stored or checked | Failure |
|---|---|---|
| Register | Salt and scrypt hash | Nothing if the email is empty |
| Login | Recomputed hash versus the stored hash | 401 invalid credentials |
| Notes | Session id maps to an email | 401 login |
The command
Login returns the cookie flags
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Notes without a session
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Notes with the seeded session
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Login answers 200 and the body includes HttpOnly, Secure, and SameSite=Lax. Notes with no session is 401. Notes with session sid-ada is 200 and includes Milk.
Guided challenge
Require the known session
The notes guard currently treats any session query as enough. This request uses session sid-stranger, which is not in the map. Look the id up and return 401 when the row is missing.
Require the known session
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
401 login
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.