link
fastapi/oauth2

OAuth2 Password Flow

Implement authentication

security
authentication

Command

Examples

Token-based authentication

from fastapi.security import OAuth2PasswordBearer
  
  oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
  
  @app.get("/users/me")
  async def read_current_user(token: str = Depends(oauth2_scheme)):
      user = decode_token(token)
      return user