Adding all fastapi_users routes incluing Google and Discord

This commit is contained in:
2025-04-03 19:24:10 +02:00
parent a0f2284efa
commit b89bb484b7
8 changed files with 25 additions and 12 deletions

View File

@@ -1,20 +1,21 @@
import os
import uuid
from beanie import PydanticObjectId
from fastapi import Depends
from fastapi_users import UUIDIDMixin, BaseUserManager
from fastapi_users import UUIDIDMixin, BaseUserManager, FastAPIUsers, schemas
from fastapi_users.authentication import AuthenticationBackend, BearerTransport
from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy
from fastapi_users_db_beanie.access_token import BeanieBaseAccessTokenDocument, BeanieAccessTokenDatabase
from httpx_oauth.clients.google import GoogleOAuth2
from fastapi_users.fastapi_users import get_oauth_router
from httpx_oauth.clients.discord import DiscordOAuth2
from hub.user import User, get_user_db
SECRET = os.getenv("FASTAPI_USERS_SECRET")
google_oauth_client = GoogleOAuth2(os.getenv("GOOGLE_CLIENT_ID"), os.getenv("GOOGLE_CLIENT_SECRET"))
discord_oauth_client = DiscordOAuth2(os.getenv("DISCORD_CLIENT_ID"), os.getenv("DISCORD_CLIENT_SECRET"))
TOKEN_LIFETIME = 3600
@@ -49,4 +50,12 @@ auth_backend = AuthenticationBackend(
get_strategy=get_database_strategy,
)
oauth_router = get_oauth_router(google_oauth_client, auth_backend, get_user_manager, SECRET)
fastapi_users = FastAPIUsers[User, PydanticObjectId](get_user_manager, [auth_backend])
auth_router = fastapi_users.get_auth_router(auth_backend, requires_verification=True)
register_router = fastapi_users.get_register_router(schemas.BaseUser, schemas.BaseUserCreate)
password_router = fastapi_users.get_reset_password_router()
verification_router = fastapi_users.get_verify_router(schemas.BaseUser)
users_router = fastapi_users.get_users_router(schemas.BaseUser, schemas.BaseUserUpdate)
google_oauth_router = fastapi_users.get_oauth_router(google_oauth_client, auth_backend, SECRET)
discord_oauth_router = fastapi_users.get_oauth_router(discord_oauth_client, auth_backend, SECRET)