Implement OAuth2 Password Flow in FastAPI

Tests FastAPI security integration and stateless auth patterns. A strong answer covers the POST /token endpoint returning a JWT, the OAuth2PasswordBearer dependency, and get_current_user decoding the JWT sub.
What's really being asked
This question evaluates whether you can assemble FastAPI's security primitives into a production-ready password flow. The interviewer cares about your understanding of stateless authentication, password hashing, dependency injection, and the exact contract between the token endpoint and the Bearer dependency. It also surfaces whether you treat security as a first-class design concern or an afterthought.
The full answer
First, the POST /token path operation receives an OAuth2PasswordRequestForm, which parses the username and password from application/x-www-form-urlencoded data. Second, the endpoint verifies the submitted password against a stored hash using a library like pwdlib, and only on success does it create a JWT with a subject claim containing the username. Third, the endpoint returns a JSON payload with access_token and token_type set to bearer. Fourth, protected routes depend on OAuth2PasswordBearer with a tokenUrl pointing at the token endpoint, which extracts the Authorization header containing the Bearer token. Fifth, a reusable get_current_user dependency decodes the JWT using the secret key and algorithm, validates the sub claim, and returns the user object to the path operation.
The mistakes people make
Storing or comparing passwords in plain text is an immediate rejection. Another red flag is omitting the OAuth2PasswordBearer dependency and instead manually parsing headers. Some candidates describe a session cookie flow rather than the stateless JWT pattern the question asks for. Confusing the resource owner password credentials flow with authorization code flow or client credentials flow also signals weak OAuth2 knowledge.
What usually comes next
The interviewer may ask how you would refresh tokens without re-prompting for the password, how to revoke tokens when the JWT is stateless, or how to add OAuth2 scopes to restrict endpoint access. They might also probe how you would rotate the signing key or store secrets outside the codebase.
A concrete example
Imagine a login endpoint at POST /token. The client sends username=alice and password=secret. FastAPI injects OAuth2PasswordRequestForm. The endpoint hashes the incoming password with pwdlib and compares it to the stored hash for alice. On match, it builds a JWT with sub set to alice and exp set to fifteen minutes. The client receives an access_token and token_type bearer. On a subsequent GET /users/me request, the client sends an Authorization header containing the Bearer token. The OAuth2PasswordBearer dependency extracts the string, get_current_user decodes it, validates the sub against the user database, and injects the user record into the handler.
Interview question
Which step must occur inside the POST /token endpoint before returning an access token?
- a.The submitted password is verified against a stored hash using a library like pwdlibCorrect
- b.The get_current_user function decodes the JWT sub claim to identify the user
- c.The OAuth2PasswordBearer dependency validates the Authorization header
- d.A server-side session is created to track the authenticated client state
Why? this is the answer
The token endpoint must verify the submitted password against a stored hash before issuing a JWT. Creating a server-side session is wrong because this flow is stateless, and get_current_user is used on subsequent protected routes, not during token creation.
Just read this? Test yourself on what you have been reading.
Read the original → fastapi.tiangolo.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.
See open roles