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:

http
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

GET/v1/me

The caller's account

Response 200 OK
  • idintegerrequired
  • emailstringrequired
  • rolestringrequired
  • restaurant_idintegernullrequired
Response 401

unauthorized

Acceptance criteria

  • A customer with a valid access token receives HTTP 200 from GET /v1/me. The response contains a positive id, the email used during signup, role set to customer, and restaurant_id explicitly set to null. A restaurant account registered with restaurant_id set to 3 receives HTTP 200 with role set to restaurant and restaurant_id set to 3.
  • Calling GET /v1/me without an Authorization header returns HTTP 401 with error set to unauthorized.
  • Every tested invalid credential returns HTTP 401 with error set to unauthorized. This includes a Basic authorization header, an empty Bearer value, Bearer not-a-jwt, Bearer immediately followed by the access token without a space, an expired HS256 token, an HS384 token, a token with alg: none, an HS256 token signed with an-attacker-secret-that-is-32-bytes!, a token whose payload was changed from customer to restaurant without a new signature, and a refresh token used as a bearer token.
  • Without an Authorization header, GET /healthz returns HTTP 200. GET /v1/restaurants with the test's centre coordinates, radius_km set to 5 and limit set to 1 returns HTTP 200. POST /v1/auth/login with an unknown email and password x returns HTTP 401 with error set to invalid_credentials, not unauthorized.

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/me accurate 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:

bash
cd backend && make up
cd backend && make test W=W3B2

Read the first failing assertion to identify whether token verification, caller identity, account data or public-route access differs from the expected behaviour.

Finished Exercise 3.5?

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

Order creation

Up next · Module 2 · 5 items · about 4 h 15 min

Start module 2

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Verify bearer tokens and reject invalid credentials consistently.
  2. Make the verified principal available to request handlers.
  3. Implement GET /v1/me using the authenticated caller's identity.
  4. Return customer and restaurant account details with the required JSON fields.
  5. Protect /v1/me while keeping public routes accessible without a token.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W3B2

Tests

  1. 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/me
    Headers
    AuthorizationBearer {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/me with their access token, then sign up a restaurant account with restaurant_id 3 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, role customer, and restaurant_id explicitly set to null. The restaurant response has role restaurant and restaurant_id 3.

    TestW3B2_MeReturnsTheCallersAccount in backend/e2e/w3_auth_test.go