Problem statement
Build a sign-in endpoint that reads a username and password from a submitted form, answers with the member behind them, and turns away a pair that does not match.
Sample Data
The register in app/store.py holds three members. Here is one member record:
{
"username": "avery",
"password": "trellis-9",
"display_name": "Avery Quinn"
}
API Endpoints to Implement
POST /login
- username and password arrive as form fields, text, both required. They are submitted as a form, never as JSON, so a JSON body supplies neither.
- Hand both to authenticate in app/services/members.py, which answers with the matching member or with nothing.
- Response code: 200, the body holding username and display_name.
- A wrong password and an unknown username are both answered with 401 and {"detail": "Invalid username or password"}, the same message either way.
Rejected requests
A missing field is answered with 422 and one detail entry per absent field, carrying loc of ["body", "field name"], an input of null, and the type and msg reported for the rule. A JSON body reports both fields absent.
The fields must be declared on the handler rather than read out of the request, since the service publishes the contract at GET /openapi.json, where the request body is required, is offered as a form and not as JSON, and points at a named schema of its own listing both fields as required text.
Sample Requests & Responses
POST /login
Request:
username=avery&password=trellis-9
Response (200 OK):
{
"username": "avery",
"display_name": "Avery Quinn"
}
POST /login
Request:
username=avery&password=quartz-42
Response (401 Unauthorized):
{
"detail": "Invalid username or password"
}
POST /login, the same credentials sent as a JSON body
Response (422 Unprocessable Entity):
{
"detail": [
{
"type": "missing",
"loc": ["body", "username"],
"msg": "Field required",
"input": null
},
{
"type": "missing",
"loc": ["body", "password"],
"msg": "Field required",
"input": null
}
]
}
