GET /v1/me: caller account
You build GET /v1/me to return the authenticated caller's account details using a verified bearer token. Invalid credentials return the same 401 unauthorized response.
Ticket
Build GET /v1/me and bearer-token middleware that verifies the 15-minute Access token issued in W3B1. The middleware must reject invalid credentials before the handler runs and put the verified caller's identity into the request context.
The Week 4 app needs this endpoint to retrieve the signed-in user's account. The order endpoints in W3B4 and W3B5 will also depend on the same authenticated caller.
For example, a request looks like:
GET /v1/me
Authorization: Bearer <access_token>A successful request returns HTTP 200 with the account belonging to that token. A customer has restaurant_id explicitly set to null.
Endpoint
The caller's account
idintegerrequiredemailstringrequiredrolestringrequiredrestaurant_idintegernullrequired
unauthorized
Acceptance criteria
- A customer with a valid access token receives HTTP 200 from
GET /v1/me. The response contains a positiveid, the email used during signup,roleset tocustomer, andrestaurant_idexplicitly set tonull. A restaurant account registered withrestaurant_idset to3receives HTTP 200 withroleset torestaurantandrestaurant_idset to3. - Calling
GET /v1/mewithout an Authorization header returns HTTP 401 witherrorset tounauthorized. - Every tested invalid credential returns HTTP 401 with
errorset tounauthorized. This includes aBasicauthorization header, an emptyBearervalue,Bearer not-a-jwt,Bearerimmediately followed by the access token without a space, an expired HS256 token, an HS384 token, a token withalg: none, an HS256 token signed withan-attacker-secret-that-is-32-bytes!, a token whose payload was changed fromcustomertorestaurantwithout a new signature, and a refresh token used as a bearer token. - Without an Authorization header,
GET /healthzreturns HTTP 200.GET /v1/restaurantswith the test's centre coordinates,radius_kmset to5andlimitset to1returns HTTP 200.POST /v1/auth/loginwith an unknown email and passwordxreturns HTTP 401 witherrorset toinvalid_credentials, notunauthorized.
Constraints
- Protected requests use
Authorization: Bearer <access_token>. - Access tokens must be valid, unexpired JWTs signed with HS256 using
JWT_SECRET. - All tested authentication failures return HTTP 401 with error code
unauthorized.
Design direction
Keep token verification and caller identity in the auth package. Keep GET /v1/me in the existing auth handlers. Let the router distinguish public routes from protected routes in one place.
A typed context value is a good way to pass the verified caller from middleware to the handler. Keep the context key private and expose a small accessor. Any layout that meets the contract passes.
Think about these trade-offs:
- Token claims versus account data: use the verified JWT to establish the caller's identity, but query MySQL for account details. This keeps
GET /v1/meaccurate when account data changes. - Authentication versus authorization: successful verification establishes the caller. Later endpoints can use the caller's role to decide which operations they may perform.
- Public versus protected routes: protect only routes that require an authenticated caller. Keep
/healthz,/v1/auth/*and the Week 2 restaurant routes accessible without a bearer token.
How to check your work
Run these commands from the repository:
cd backend && make up
cd backend && make test W=W3B2Read the first failing assertion to identify whether token verification, caller identity, account data or public-route access differs from the expected behaviour.
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.
Account authentication
5 items
- 3.1Password hashing and random secretsnot completed
- 3.2JWT and refresh token rotationnot completed
- 3.3POST /v1/auth/*: signup, login and refreshnot completed
- 3.4Bearer middleware and request contextnot completed
- 3.5GET /v1/me: caller accountnot completed
Up next · Module 2 · 5 items · about 4 h 15 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Verify bearer tokens and reject invalid credentials consistently.
- Make the verified principal available to request handlers.
- Implement
GET /v1/meusing the authenticated caller's identity. - Return customer and restaurant account details with the required JSON fields.
- Protect
/v1/mewhile keeping public routes accessible without a token.
Commands
cd backend && make upcd backend && make test W=W3B2Tests
A valid access token identifies the caller and returns the caller's current account details, including a null restaurant ID for a customer.
GET/v1/meHeadersAuthorization Bearer {access_token} Response 200 OK{ "id": 17, "email": "asha@test.local", "role": "customer", "restaurant_id": null }Input Sign up a customer and call
GET /v1/mewith their access token, then sign up a restaurant account withrestaurant_id3 and call the same endpoint with its access token.Expected Both requests return HTTP 200. The customer response has a positive
id, the signup email,rolecustomer, andrestaurant_idexplicitly set tonull. The restaurant response hasrolerestaurantandrestaurant_id3.TestW3B2_MeReturnsTheCallersAccountinbackend/e2e/w3_auth_test.go