def get_users_router(db): users_collection = db["users"] user_db = MongoDBUserDatabase(UserDB, users_collection) fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) users_router = APIRouter() users_router.include_router( fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"], ) users_router.include_router(fastapi_users.get_register_router(), prefix="/auth", tags=["auth"]) users_router.include_router( fastapi_users.get_reset_password_router(settings.JWT_SECRET_KEY), prefix="/auth", tags=["auth"], ) users_router.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"]) return users_router
async def test_app_client( mock_user_db, mock_authentication, oauth_client, get_test_client) -> AsyncGenerator[httpx.AsyncClient, None]: fastapi_users = FastAPIUsers( mock_user_db, [mock_authentication], User, UserCreate, UserUpdate, UserDB, ) app = FastAPI() app.include_router(fastapi_users.get_register_router()) app.include_router(fastapi_users.get_reset_password_router("SECRET")) app.include_router(fastapi_users.get_auth_router(mock_authentication)) app.include_router(fastapi_users.get_oauth_router(oauth_client, "SECRET")) app.include_router(fastapi_users.get_users_router(), prefix="/users") @app.get("/current-user") def current_user(user=Depends(fastapi_users.get_current_user)): return user @app.get("/current-active-user") def current_active_user(user=Depends( fastapi_users.get_current_active_user)): return user @app.get("/current-superuser") def current_superuser(user=Depends(fastapi_users.get_current_superuser)): return user @app.get("/optional-current-user") def optional_current_user(user=Depends( fastapi_users.get_optional_current_user)): return user @app.get("/optional-current-active-user") def optional_current_active_user( user=Depends(fastapi_users.get_optional_current_active_user), ): return user @app.get("/optional-current-superuser") def optional_current_superuser( user=Depends(fastapi_users.get_optional_current_superuser), ): return user async for client in get_test_client(app): yield client
async def startup(): await db.connect_to_database(path=DATABASE_URL) disc_db = db.client[DATABASE_NAME] global fs fs = AsyncIOMotorGridFSBucket(disc_db) global song_db song_db = disc_db.get_collection(SONGS_DB) global mix_db mix_db = disc_db.get_collection(MIX_DB) user_col = disc_db[USER_DB] global user_db user_db = MongoDBUserDatabase(UserDB, user_col) global fastapi_users fastapi_users = FastAPIUsers(user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB) app.include_router(fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]) app.include_router( fastapi_users.get_register_router(after_register=on_after_register), prefix="/auth", tags=["auth"]) app.include_router( fastapi_users.get_reset_password_router( SECRET, after_forgot_password=on_after_forgot_password), prefix="/auth", tags=["auth"], ) app.include_router( fastapi_users.get_verify_router( SECRET, after_verification_request=after_verification_request), prefix="/auth", tags=["auth"], ) app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"])
app = FastAPI() fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) app.include_router(fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]) app.include_router(fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]) app.include_router( fastapi_users.get_reset_password_router( SECRET, after_forgot_password=on_after_forgot_password), prefix="/auth", tags=["auth"], ) app.include_router( fastapi_users.get_verify_router( SECRET, after_verification_request=after_verification_request), prefix="/auth", tags=["auth"], ) app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"])
app.include_router( fastapi_users.get_users_router(), prefix="/auth/users", tags=["auth"] ) # Add route for Reset Password utility """ Forgot Password POST /auth/users/forgot-password Reset Password POST /auth/users/reset-password """ app.include_router( fastapi_users.get_reset_password_router("SECRET"), prefix="/auth/users", tags=["auth"] ) # --- Custom Unprotected Routes Template -------------------------------------- """ The below templates can be used for creating any Rest APIs that are independent of user's authenticaion state (logged in or logged out). Hence, these API calls don't necessarily require a user to be logged in. Please read Mongo Motor docs to perform async DB operations. Learn more https://motor.readthedocs.io/en/stable/
app = FastAPI() fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) app.include_router(fastapi_users.get_auth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"]) app.include_router(fastapi_users.get_register_router(), prefix="/auth", tags=["auth"]) app.include_router(fastapi_users.get_reset_password_router(APP_SECRET), prefix="/auth", tags=["auth"]) app.include_router(fastapi_users.get_verify_router(APP_SECRET), prefix="/auth", tags=["auth"]) app.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"]) app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
from fastapi import Depends, Response from fastapi_users import FastAPIUsers from fastapi_users.authentication import JWTAuthentication from ..config import settings from .models import user_db, User, UserCreate, UserUpdate, UserDB jwt_authentication = JWTAuthentication(secret=settings.jwt_secret, lifetime_seconds=3600, tokenUrl="/auth/login") accounts = FastAPIUsers( db=user_db, auth_backends=[jwt_authentication], user_model=User, user_create_model=UserCreate, user_update_model=UserUpdate, user_db_model=UserDB, ) authrouter = accounts.get_auth_router(jwt_authentication) authresetpasswordrouter = accounts.get_reset_password_router(reset_password_token_secret=settings.reset_password_secret) registerrouter = accounts.get_register_router() usersrouter = accounts.get_users_router() @authrouter.post("/refresh-token") async def refresh_token(response: Response, user=Depends(accounts.get_current_active_user)): return await jwt_authentication.get_login_response(user, response)
from fastapi import APIRouter from fastapi_users import FastAPIUsers from app.models.user import User, UserCreate, UserDB, UserUpdate from app.users.auth_backend import authentication_backend from app.users.user_manager import get_user_manager fastapi_users = FastAPIUsers( get_user_manager, [authentication_backend], User, UserCreate, UserUpdate, UserDB, ) auth_router = APIRouter(prefix="/auth", tags=["auth"]) auth_router.include_router( fastapi_users.get_auth_router(authentication_backend), prefix="/jwt") auth_router.include_router(fastapi_users.get_register_router()) auth_router.include_router(fastapi_users.get_reset_password_router()) auth_router.include_router(fastapi_users.get_verify_router()) users_router = APIRouter() users_router.include_router(auth_router) users_router.include_router(fastapi_users.get_users_router(), prefix="/users", tags=["users"]) current_active_user = fastapi_users.current_user(active=True)
UserUpdate, UserDB, ) app.include_route( fastapi_users.get_oauth_router(jwt_authentication), prefix="/auth/jwt", tags=["auth"], ) app.include_route( # Depois de registrar que chama a função on_after_register fastapi_users.get_register_router(on_after_register), prefix="/auth", tags=["auth"]) app.include_route(fastapi_users.get_reset_password_router( SECRET, after_forgot_password=on_after_forgot_password()), prefix="/auth", tags=["auth"]) app.include_route(fastapi_users.get_users_router(), prefix="/users", tags=["auth"]) google_oauth_router = fastapi_users.get_auth_router( google_oauth_client, SECRET, after_register=on_after_register) app.include_route(fastapi_users.get_users_router(), prefix="/users", tags=["users"]) facebook_oauth_routher = fastapi_users.get_auth_router(
from fastapi_users import FastAPIUsers from fastapi_users.authentication import JWTAuthentication from {{ cookiecutter.project_slug }}.adapters.db import repo_sqlalchemy, models from {{ cookiecutter.project_slug }}.config import config from {{ cookiecutter.project_slug }} import domain jwt_authentication = JWTAuthentication( secret=config.TOKEN_SECRET, lifetime_seconds=3600, tokenUrl=config.TOKEN_URL, ) users = FastAPIUsers( repo_sqlalchemy.fastapi_user, [jwt_authentication], domain.User, domain.commands.CreateUser, domain.commands.UpdateUser, models.UserDB, ) auth_router = users.get_auth_router(jwt_authentication) register_router = users.get_register_router() reset_password_router = users.get_reset_password_router(config.TOKEN_SECRET) verify_router = users.get_verify_router(config.TOKEN_SECRET) users_router = users.get_users_router()
Get "_id" user profile GET "/auth/users/" Update "_id" user profile PATCH "/auth/users/{id}" Delete "_id" user profile DELETE "/auth/users/{id}" """ app.include_router(fastapi_users.get_users_router(), prefix="/auth/users", tags=["auth"]) # Add route for Reset Password utility """ Forgot Password POST /auth/users/forgot-password Reset Password POST /auth/users/reset-password """ app.include_router(fastapi_users.get_reset_password_router("SECRET"), prefix="/auth/users", tags=["auth"]) # --- Custom Unprotected Routes Template -------------------------------------- """ The below templates can be used for creating any Rest APIs that are independent of user's authenticaion state (logged in or logged out). Hence, these API calls don't necessarily require a user to be logged in. Please read Mongo Motor docs to perform async DB operations. Learn more https://motor.readthedocs.io/en/stable/ """
from fastapi_users import FastAPIUsers from fastapi_users.authentication import JWTAuthentication from fastapi_users.db import MongoDBUserDatabase db = CLIENT["database_name"] collection = db["users"] user_db = MongoDBUserDatabase(UserDB, collection) jwt_authentication = JWTAuthentication(secret=SECRET, lifetime_seconds=3600, tokenUrl="/auth/jwt/login") fastapi_users = FastAPIUsers( user_db, [jwt_authentication], User, UserCreate, UserUpdate, UserDB, ) AUTH_ROUTER = fastapi_users.get_auth_router(jwt_authentication) REGISTER_ROUTER = fastapi_users.get_register_router() RESET_PASSWORD_ROUTER = fastapi_users.get_reset_password_router(SECRET) VERIFY_ROUTER = fastapi_users.get_verify_router(SECRET) USERS_ROUTER = fastapi_users.get_users_router() GOOGLE_OAUTH_ROUTER = fastapi_users.get_oauth_router( oauth_client=GOOGLE_OAUTH_CLIENT, state_secret=SECRET, redirect_url="http://localhost:8080/auth/google/callback", )