POST /v1/auth/*: signup, login and refresh
You build POST /v1/auth/* to register accounts, exchange passwords for token pairs, and rotate refresh tokens. Each refresh token works once.
Ticket
Add the users table through a Database migration and build POST /v1/auth/signup, POST /v1/auth/login, and POST /v1/auth/refresh. Signup stores a bcrypt password hash and returns a signed JWT Access token with an opaque Refresh token. Login verifies the password, and refresh replaces the refresh token.
The app built in Week 4 needs these routes to authenticate users. W3B2 uses the access token to authenticate requests through bearer auth middleware.
For example, a successful signup uses a request like:
{
"email": "learner@example.test",
"password": "correct horse battery staple"
}It returns HTTP 201 with a token pair. The access token is a verifiable JWT, and the refresh token is a new opaque credential.
Endpoints
Create an account and log it in
emailstringrequiredpasswordstringrequiredrole"customer" | "restaurant" | "rider"restaurant_idinteger
access_tokenstringrequiredA JWT signed with HS256 and JWT_SECRET, carrying exp. Its other claims are yours to choose.
refresh_tokenstringrequired32 random bytes as 64 lowercase hex characters.
token_type"Bearer"requiredexpires_in900required
invalid_bodyinvalid_parameter
email_taken
Exchange email and password for a token pair
emailstringrequiredpasswordstringrequired
access_tokenstringrequiredA JWT signed with HS256 and JWT_SECRET, carrying exp. Its other claims are yours to choose.
refresh_tokenstringrequired32 random bytes as 64 lowercase hex characters.
token_type"Bearer"requiredexpires_in900required
invalid_body
invalid_credentials
Rotate a refresh token
refresh_tokenstringrequired
access_tokenstringrequiredA JWT signed with HS256 and JWT_SECRET, carrying exp. Its other claims are yours to choose.
refresh_tokenstringrequired32 random bytes as 64 lowercase hex characters.
token_type"Bearer"requiredexpires_in900required
invalid_body
invalid_refresh_token
Acceptance criteria
- Signup with a valid email and password returns HTTP 201. The access token is a verifiable HS256 JWT signed with
JWT_SECRETand expiring about 15 minutes ahead. The refresh token contains 64 lowercase hexadecimal characters.token_typeisBearerandexpires_inis900. - The new user's
users.password_hashmatches the bcrypt hash format and has a cost of at least 10. - Signup trims surrounding whitespace and lowercases the email before storing it. Exactly one user row exists under the normalized email, and login using the uppercase form returns HTTP 200 with a valid token pair.
- A second signup using the same email in uppercase returns HTTP 409 with error
email_taken. - Signup with email
not-an-emailorBob <bob@test.local>returns HTTP 400 with errorinvalid_parameterand fieldemail. An empty password or a password consisting of 73acharacters returns HTTP 400 with errorinvalid_parameterand fieldpassword. Roleadminreturns HTTP 400 with errorinvalid_parameterand fieldrole. Rolerestaurantwithoutrestaurant_id, or rolecustomerwithrestaurant_idset to1, returns HTTP 400 with errorinvalid_parameterand fieldrestaurant_id. - Sending the raw body
email=a@b.cto signup returns HTTP 400 with errorinvalid_body. - Login with a registered email and the correct password returns HTTP 200 with a valid token pair. The response contains a verifiable access token, a non-empty refresh token,
token_typeset toBearer, andexpires_inset to900. Sending that refresh token to the refresh endpoint returns HTTP 200 with another valid token pair. - Login with password
wrong passwordreturns HTTP 401 with errorinvalid_credentialsfor both a registered email and an unknown email. - Refreshing a valid token returns HTTP 200 with a valid token pair whose refresh token differs from the original. The replacement token also works for another refresh request, returning HTTP 200. Reusing the original token or submitting an unknown token made from 32 repetitions of
abreturns HTTP 401 with errorinvalid_refresh_token.
Constraints
- Passwords must contain between 1 and 72 bytes.
- Stored bcrypt hashes must have a cost of at least 10.
- Access tokens must be signed with HS256 using
JWT_SECRETand expire about 15 minutes after issue. - Refresh tokens must contain 64 lowercase hexadecimal characters.
Design direction
A sensible layout is to keep authentication concerns in an internal/auth area. Separate password hashing, access-token issuing, refresh-token storage, and HTTP handlers. Keep the users schema change in db/migrations. Handlers should validate requests, coordinate these responsibilities, and return the API contract's responses. Any layout that meets the contract passes.
Think about these trade-offs:
- Normalize email before both storing it and looking it up. Use a MySQL unique constraint to handle duplicate signups, including concurrent requests.
- Consider a dummy bcrypt hash for unknown email addresses. It makes the password-verification path more comparable for unknown accounts and incorrect passwords.
- Keep the access token stateless and the refresh token as an opaque credential stored in Redis. Redis TTLs limit how long refresh credentials remain usable.
- Refresh requires a one-use operation. An atomic consume avoids the race in which two requests both check the same token before either deletes it. MySQL and Redis do not share a transaction, so consider what happens when one operation succeeds and the next fails.
How to check your work
Run these commands from the repository:
cd backend && make up
cd backend && make test W=W3B1Read the first failing assertion in the test output. It identifies the request, response, or stored value that differs from the expected behaviour. Fix that failure before investigating later assertions.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Create and reverse the
userstable migration. - Store passwords as bcrypt hashes.
- Normalize email addresses and reject duplicate accounts.
- Implement signup and login token responses.
- Return consistent errors for invalid credentials and input.
- Rotate refresh tokens and reject reused or unknown tokens.
Commands
cd backend && make upcd backend && make test W=W3B1Tests
Signup returns HTTP 201 with a verifiable token pair containing the required token fields and expiry.
POST/v1/auth/signupBody raw JSON{ "email": "asha@test.local", "password": "correct horse battery staple" }Response 201{ "access_token": "eyJhbGciOiJIUzI1NiIs…", "refresh_token": "9f2c…(64 hex characters)", "token_type": "Bearer", "expires_in": 900 }Input
POST /v1/auth/signupwith a unique email and passwordcorrect horse battery staple.Expected HTTP 201; an HS256 JWT signed with
JWT_SECRETexpiring about 15 minutes ahead; a 64-character lowercase hexadecimal refresh token;token_typeBearer;expires_in900.TestW3B1_SignupReturns201WithAVerifiableTokenPairinbackend/e2e/w3_auth_test.go