mirror of
				https://github.com/fastapi-users/fastapi-users.git
				synced 2025-11-04 14:45:50 +08:00 
			
		
		
		
	* Implement Transport classes * Implement authentication strategy classes * Revamp authentication with Transport and Strategy * Revamp strategy and OAuth so that they can use a callable dependency * Update docstring * Make ErrorCode a proper Enum and cleanup unused OpenAPI utils * Remove useless check * Tweak typing in authenticator * Update docs * Improve logout/destroy token logic * Update docs * Update docs * Update docs and full examples * Apply formatting to examples * Update OAuth doc and examples * Add migration doc * Implement Redis session token * Add Redis Session documentation * RedisSession -> Redis * Fix links in docs
		
			
				
	
	
		
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import os
 | 
						|
from typing import Optional
 | 
						|
 | 
						|
from fastapi import Depends, Request
 | 
						|
from fastapi_users import BaseUserManager, FastAPIUsers
 | 
						|
from fastapi_users.authentication import (
 | 
						|
    AuthenticationBackend,
 | 
						|
    BearerTransport,
 | 
						|
    JWTStrategy,
 | 
						|
)
 | 
						|
from fastapi_users.db import SQLAlchemyUserDatabase
 | 
						|
from httpx_oauth.clients.google import GoogleOAuth2
 | 
						|
 | 
						|
from app.db import get_user_db
 | 
						|
from app.models import User, UserCreate, UserDB, UserUpdate
 | 
						|
 | 
						|
SECRET = "SECRET"
 | 
						|
 | 
						|
 | 
						|
google_oauth_client = GoogleOAuth2(
 | 
						|
    os.environ["GOOGLE_OAUTH_CLIENT_ID"],
 | 
						|
    os.environ["GOOGLE_OAUTH_CLIENT_SECRET"],
 | 
						|
)
 | 
						|
 | 
						|
 | 
						|
class UserManager(BaseUserManager[UserCreate, UserDB]):
 | 
						|
    user_db_model = UserDB
 | 
						|
    reset_password_token_secret = SECRET
 | 
						|
    verification_token_secret = SECRET
 | 
						|
 | 
						|
    async def on_after_register(self, user: UserDB, request: Optional[Request] = None):
 | 
						|
        print(f"User {user.id} has registered.")
 | 
						|
 | 
						|
    async def on_after_forgot_password(
 | 
						|
        self, user: UserDB, token: str, request: Optional[Request] = None
 | 
						|
    ):
 | 
						|
        print(f"User {user.id} has forgot their password. Reset token: {token}")
 | 
						|
 | 
						|
    async def on_after_request_verify(
 | 
						|
        self, user: UserDB, token: str, request: Optional[Request] = None
 | 
						|
    ):
 | 
						|
        print(f"Verification requested for user {user.id}. Verification token: {token}")
 | 
						|
 | 
						|
 | 
						|
async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(get_user_db)):
 | 
						|
    yield UserManager(user_db)
 | 
						|
 | 
						|
 | 
						|
bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
 | 
						|
 | 
						|
 | 
						|
def get_jwt_strategy() -> JWTStrategy:
 | 
						|
    return JWTStrategy(secret=SECRET, lifetime_seconds=3600)
 | 
						|
 | 
						|
 | 
						|
auth_backend = AuthenticationBackend(
 | 
						|
    name="jwt",
 | 
						|
    transport=bearer_transport,
 | 
						|
    get_strategy=get_jwt_strategy,
 | 
						|
)
 | 
						|
fastapi_users = FastAPIUsers(
 | 
						|
    get_user_manager,
 | 
						|
    [auth_backend],
 | 
						|
    User,
 | 
						|
    UserCreate,
 | 
						|
    UserUpdate,
 | 
						|
    UserDB,
 | 
						|
)
 | 
						|
 | 
						|
current_active_user = fastapi_users.current_user(active=True)
 |