Booking backend project · Chapter 3 of 12
Signup and login
By the end of this chapter you can store passwords the way real backends do (a salted bcrypt hash, never the password), sign members up with one account per email, turn Postgres's "already exists" into a clear 409, and log people in without telling a stranger which emails have accounts. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Accounts
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.
Why you need this
Every booking belongs to someone. Diwali week's bookings, holds and refunds all need a member behind them, and Asha's admin pages need to know that the person adding showtimes is Asha. So Parda needs accounts: a member signs up with an email and a password, and logs in with them later.
The data is already there: the users table has an email that is unique and a password_hash column, and the seed made three people. What is missing is the code. The routes exist (POST /auth/signup, POST /auth/login), but the service they call is empty, so every request that reaches it answers 500. "Sign up Ravi" sent with curl (the status in brackets):
$ curl -s -w " [%{http_code}]\n" -X POST localhost:3000/auth/signup -H 'content-type: application/json' -d '{"email":"ravi.kulkarni@example.com","name":"Ravi Kulkarni","password":"ravi-member-2026"}'
{"error":{"code":"internal","message":"Something went wrong on our side"}} [500]
and the server log says why:
Error: Task 2: signup is not written yet
at signup (/workspace/chapters/03-signup-and-login/src/modules/auth/auth.service.ts:55:9)
at signupHandler (/workspace/chapters/03-signup-and-login/src/modules/auth/auth.controller.ts:7:22)
...
Getting it nearly right is not enough here. A table of plain passwords is the worst thing a breach can leak, because people reuse passwords: Neha's Parda password may well be her email password too. And a login that says "no account with that email" tells anyone which of their friends' emails are Parda members.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
password.ts: hash and check a password
Write two functions in src/modules/auth/password.ts with bcryptjs (imported as bcrypt). hashPassword(password) returns a bcrypt hash of the password with the cost BCRYPT_COST (10): a string that starts with $2b$10$ (or $2a$10$), different every time for the same password because bcrypt adds a random salt. verifyPassword(password, hash) returns true when the password is the one the hash was made from, and false otherwise.
The Check hashes a password twice, checks the right and a wrong password against the hash, and checks Asha's lab password, asha-admin-2026, against her hash in seed-data.ts: that must be true.
- Task 2
auth.service.ts: sign up a member
Write signup(input) in src/modules/auth/auth.service.ts. input is { email, name, password }. Insert one row into users with the email trimmed and in lower case (the given normalizeEmail), the name trimmed, and passwordHash from hashPassword; leave the role to its default, user. Return the new person as { id, email, name, role }, never with the password or its hash (the given publicColumns are exactly those four).
users.email is unique, so a second signup with the same email in any case makes Postgres refuse the insert: catch that and throw conflict('email_taken', 'An account with that email already exists'), which the error handler answers with 409. Throw any other error on unchanged.
The Check signs up " Ravi.Kulkarni@Example.com " and wants ravi.kulkarni@example.com stored with a bcrypt hash, then the same email in capitals to throw that 409. Through the app, POST /auth/signup for a new member answers 201 with { user }, and with Neha's email 409 and the code email_taken.
- Task 3 · interview question
Interview: auth.service.ts, log in
Write login(input) in the same file. input is { email, password }. Find the person whose email is the normalized email, check the password with verifyPassword, and return them as { id, email, name, role }, without the hash. When no one has that email, or the password is wrong, throw unauthorized('Email or password is wrong'): a 401 with the code unauthorized and the same message for both.
The Check logs in as Neha with her email in capitals (id 2, role user) and as Asha (role admin), then wants a wrong password and an unknown email to throw the same 401. Through the app, POST /auth/login as Neha answers 200 with { user }, and with a wrong password 401 and that message.
Nikhil will ask: why does login answer the same for an unknown email and a wrong password, and why should it still run bcrypt when the email is unknown?
What you will learn
- Hashing: why a password is stored as a hash, and what makes bcrypt's hash right for it (a salt, and a cost).
- bcryptjs: making a hash and checking a password against one.
- One account per email: the unique constraint does the work; your code turns its error into a 409.
- AppError and the error handler: errors the API means to send, in one shape.
- A login that gives nothing away: one answer for every wrong attempt, and the same time to say it.
More in Accounts
Build "Signup and login" yourself
Parda sells tickets for Pune's own cinemas and stages. You build its booking API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.