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

1
api/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*/__pycache__/*

View File

@@ -1,20 +1,21 @@
import os import os
import uuid import uuid
from beanie import PydanticObjectId
from fastapi import Depends 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 import AuthenticationBackend, BearerTransport
from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy from fastapi_users.authentication.strategy import AccessTokenDatabase, DatabaseStrategy
from fastapi_users_db_beanie.access_token import BeanieBaseAccessTokenDocument, BeanieAccessTokenDatabase from fastapi_users_db_beanie.access_token import BeanieBaseAccessTokenDocument, BeanieAccessTokenDatabase
from httpx_oauth.clients.google import GoogleOAuth2 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 from hub.user import User, get_user_db
SECRET = os.getenv("FASTAPI_USERS_SECRET") SECRET = os.getenv("FASTAPI_USERS_SECRET")
google_oauth_client = GoogleOAuth2(os.getenv("GOOGLE_CLIENT_ID"), os.getenv("GOOGLE_CLIENT_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 TOKEN_LIFETIME = 3600
@@ -49,4 +50,12 @@ auth_backend = AuthenticationBackend(
get_strategy=get_database_strategy, 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)

View File

@@ -3,7 +3,8 @@ from contextlib import asynccontextmanager
from fastapi import FastAPI from fastapi import FastAPI
from hub import init_db as hub_init_db from hub import init_db as hub_init_db
from hub.auth import oauth_router from hub.auth import auth_router, register_router, password_router, verification_router, users_router, \
google_oauth_router, discord_oauth_router
if __name__ == '__main__': if __name__ == '__main__':
import uvicorn import uvicorn
@@ -20,8 +21,10 @@ async def lifespan(app: FastAPI):
app = FastAPI(root_path="/api/v1", lifespan=lifespan) app = FastAPI(root_path="/api/v1", lifespan=lifespan)
app.include_router( app.include_router(register_router, tags=["Auth"], )
oauth_router, app.include_router(auth_router, prefix="/auth", tags=["Auth"], )
prefix="/auth/google", app.include_router(google_oauth_router, prefix="/auth/google", tags=["Auth"])
tags=["auth"], app.include_router(discord_oauth_router, prefix="/auth/discord", tags=["Auth"])
) app.include_router(verification_router, prefix="/auth/verification", tags=["Auth"], )
app.include_router(users_router, prefix="/users", tags=["Users"], )
app.include_router(password_router, prefix="/users", tags=["Users"], )

View File

@@ -3,7 +3,7 @@ services:
build: build:
context: ./api context: ./api
#image: roleplay-contracts-api-dev #image: roleplay-contracts-api-dev
env_file: ".env" env_file: "./.env"
restart: always restart: always
ports: ports:
- "8000:8000" - "8000:8000"
@@ -48,7 +48,7 @@ services:
mongo: mongo:
image: mongo:latest image: mongo:latest
env_file: ".env" env_file: "./.env"
restart: always restart: always
ports: ports:
- "27017:27017" - "27017:27017"