Overview
FastAPI-Users 10.0, released on March 1, 2022, introduces dependency injection for authentication and user management.
Main Features
Dependency injection
Authentication is handled through FastAPI's dependency injection system, making configuration more modular and testable.
python
from fastapi import Depends, FastAPI
from fastapi_users import FastAPIUsers
from fastapi_users.authentication import BearerTransport, JWTStrategy
def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(secret='SECRET', lifetime_seconds=3600)
bearer_transport = BearerTransport(tokenUrl='auth/jwt/login')
# fastapi_users = FastAPIUsers[User, uuid.UUID](
# get_user_manager,
# [auth_backend],
# )
Pre-configured routes
Registration, login, and profile management routes are pre-configured and customizable via dependencies.
python
from fastapi import FastAPI
app = FastAPI()
# Automatic auth routes
# app.include_router(
# fastapi_users.get_auth_router(auth_backend),
# prefix='/auth/jwt',
# )
# app.include_router(
# fastapi_users.get_register_router(UserRead, UserCreate),
# prefix='/auth',
# )
