Ordering 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
  • Python
  • In the all labs plan

The first 2 chapters of Ordering backend project are free. This one comes with the all labs plan.

Why you need this

Every dabba belongs to someone. Launch week's orders, slot holds and Rafiq's refunds all need a member behind them, and the admin routes of chapter 5 must know that the person opening a kitchen is Bhavna. So DabbaDrop needs accounts: a member signs up with an email and a password, and logs in with them later.

The data is ready: users.email is unique, password_hash is waiting, 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:8000/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, in the terminal, says why:

INFO:     127.0.0.1:36048 - "POST /auth/signup HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
...
NotImplementedError: Task 2: signup is not written yet

Nearly right is not enough here. A table of plain passwords is the worst thing a breach can leak, because people reuse passwords: Tara's DabbaDrop password may well be her email password too. And a login that says "no account with that email" tells anyone which of their colleagues order from DabbaDrop.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    passwords.py: hash and check a password

    Write two functions in app/modules/auth/passwords.py with the bcrypt package. hash_password(password) returns a bcrypt hash of the password with a new random salt and cost COST (10), as a str: it starts $2b$10$ and is 60 characters long, and two hashes of one password differ. verify_password(password, 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 hashes, and checks Tara's password against her seeded hash.

  2. Task 2

    service.py: sign up a member

    Write signup(db, data) in app/modules/auth/service.py. data is a SignupIn (email, name, password). Make a User with the email trimmed and in lower case, the name trimmed, password_hash from hash_password(data.password) and the role "member"; add it and commit; return public_user(user) (given), a dict {id, email, name, role} without the hash.

    When the email already has an account, the commit fails with an IntegrityError on the unique constraint users_email_key: roll back and raise conflict("email_taken", ...), a 409, and the table keeps one row for that email. Read the error with the given is_unique_violation(err, "users_email_key").

    The Check signs up Ravi as "Ravi.Kulkarni@Example.COM", reads his row, signs up the same email in other letters (409 email_taken), then sends POST /auth/signup for Meera (201 with { user }) and with Tara's email (409, code email_taken).

  3. Task 3 · interview question

    Interview: service.py, log in

    Write login(db, data) in app/modules/auth/service.py. data is a LoginIn (email, password). Find the user whose email is data.email in lower case; when the password checks out with verify_password, return public_user(user). Otherwise raise unauthorized("Wrong email or password") (401): the same error for an unknown email and for a wrong password. For an unknown email, call verify_password(data.password, DUMMY_HASH) once before raising, so the answer takes as long as for a wrong password.

    The Check logs in as Tara (also with her email in capitals), tries a wrong password and an unknown email and wants both 401s to say "Wrong email or password", counts the calls to verify_password for the unknown email (one, on DUMMY_HASH), then sends POST /auth/login as Advait (200 with { user }) and with a wrong password (401).

    Kunal will ask: why one message for a wrong email and a wrong password, and why check a password nobody has against a dummy hash?

What you will learn

  • Hashing: why a password is stored as a hash, and what makes bcrypt right for it (a salt and a cost).
  • The bcrypt package: making a hash and checking a password against one, in bytes.
  • One account per email: the unique constraint does the work; your code turns its error into a 409.
  • AppError and FastAPI's exception handlers: the 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

DabbaDrop delivers tiffins across Mumbai. You build its ordering 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 Ordering backend project are free. This one comes with the all labs plan.