mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 19:30:47 +08:00
Revamp authentication routes structure (#201)
* Fix #68: use makefun to generate dynamic dependencies * Remove every Starlette imports * Split every routers and remove event handlers * Make users router optional * Pass after_update handler to get_users_router * Update documentation * Remove test file * Write migration doc for splitted routers
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi_users import FastAPIUsers, models
|
||||
from fastapi_users.authentication import JWTAuthentication
|
||||
from fastapi_users.db import (
|
||||
@ -7,7 +7,6 @@ from fastapi_users.db import (
|
||||
TortoiseUserDatabase,
|
||||
)
|
||||
from httpx_oauth.clients.google import GoogleOAuth2
|
||||
from starlette.requests import Request
|
||||
from tortoise import fields
|
||||
from tortoise.contrib.starlette import register_tortoise
|
||||
|
||||
@ -46,24 +45,36 @@ user_db = TortoiseUserDatabase(UserDB, UserModel, OAuthAccountModel)
|
||||
app = FastAPI()
|
||||
register_tortoise(app, db_url=DATABASE_URL, modules={"models": ["test"]})
|
||||
|
||||
auth_backends = [
|
||||
JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
|
||||
]
|
||||
|
||||
fastapi_users = FastAPIUsers(
|
||||
user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET,
|
||||
)
|
||||
app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
||||
|
||||
google_oauth_router = fastapi_users.get_oauth_router(google_oauth_client, SECRET)
|
||||
app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"])
|
||||
|
||||
|
||||
@fastapi_users.on_after_register()
|
||||
def on_after_register(user: User, request: Request):
|
||||
def on_after_register(user: UserDB, request: Request):
|
||||
print(f"User {user.id} has registered.")
|
||||
|
||||
|
||||
@fastapi_users.on_after_forgot_password()
|
||||
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||
def on_after_forgot_password(user: UserDB, token: str, request: Request):
|
||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||
|
||||
|
||||
jwt_authentication = JWTAuthentication(secret=SECRET, lifetime_seconds=3600)
|
||||
|
||||
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_users_router(), prefix="/users", tags=["users"])
|
||||
|
||||
google_oauth_router = fastapi_users.get_oauth_router(
|
||||
google_oauth_client, SECRET, after_register=on_after_register
|
||||
)
|
||||
app.include_router(google_oauth_router, prefix="/auth/google", tags=["auth"])
|
||||
|
Reference in New Issue
Block a user