I found the following FastAPI code for authenticating a user with their information gotten from a form:
@app.post("/token")
async def login_for_access_token(form_data:OAuth2PasswordRequestForm = Depends(),
db: Session = Depends(get_db)):
user = authenticate_user(form_data.username, form_data.password, db)
if not user:
raise token_exception()
token_expires = timedelta(minutes=20)
token = create_access_token(user.username,
user.id,
expires_delta=token_expires)
return {"token": token}
I'm struggling to understand why in form_data:OAuth2PasswordRequestForm = Depends()
, Depends()
has no parameter passed to it? I thought that the whole point of Depends()
was to be instantiated with a function that gets called before the endpoint function is called.