mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-15 11:11:16 +08:00
Fix #100: wrong event handlers usage in full examples
This commit is contained in:
@ -3,6 +3,7 @@ from fastapi import FastAPI
|
|||||||
from fastapi_users import FastAPIUsers, models
|
from fastapi_users import FastAPIUsers, models
|
||||||
from fastapi_users.authentication import JWTAuthentication
|
from fastapi_users.authentication import JWTAuthentication
|
||||||
from fastapi_users.db import MongoDBUserDatabase
|
from fastapi_users.db import MongoDBUserDatabase
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "mongodb://localhost:27017"
|
DATABASE_URL = "mongodb://localhost:27017"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -41,10 +42,10 @@ app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
@ -5,6 +5,7 @@ from fastapi_users import FastAPIUsers, models
|
|||||||
from fastapi_users.authentication import JWTAuthentication
|
from fastapi_users.authentication import JWTAuthentication
|
||||||
from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase
|
from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase
|
||||||
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "sqlite:///./test.db"
|
DATABASE_URL = "sqlite:///./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -55,12 +56,12 @@ app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from fastapi_users import FastAPIUsers, models
|
|||||||
from fastapi_users.authentication import JWTAuthentication
|
from fastapi_users.authentication import JWTAuthentication
|
||||||
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
from fastapi_users.db import TortoiseBaseUserModel, TortoiseUserDatabase
|
||||||
from tortoise.contrib.starlette import register_tortoise
|
from tortoise.contrib.starlette import register_tortoise
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "sqlite://./test.db"
|
DATABASE_URL = "sqlite://./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -43,10 +44,10 @@ app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
@ -4,6 +4,7 @@ from fastapi_users import FastAPIUsers, models
|
|||||||
from fastapi_users.authentication import JWTAuthentication
|
from fastapi_users.authentication import JWTAuthentication
|
||||||
from fastapi_users.db import MongoDBUserDatabase
|
from fastapi_users.db import MongoDBUserDatabase
|
||||||
from httpx_oauth.clients.google import GoogleOAuth2
|
from httpx_oauth.clients.google import GoogleOAuth2
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "mongodb://localhost:27017"
|
DATABASE_URL = "mongodb://localhost:27017"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -48,10 +49,10 @@ app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
@ -10,6 +10,7 @@ from fastapi_users.db import (
|
|||||||
)
|
)
|
||||||
from httpx_oauth.clients.google import GoogleOAuth2
|
from httpx_oauth.clients.google import GoogleOAuth2
|
||||||
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "sqlite:///./test.db"
|
DATABASE_URL = "sqlite:///./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -71,12 +72,12 @@ app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ from fastapi_users.db import (
|
|||||||
from httpx_oauth.clients.google import GoogleOAuth2
|
from httpx_oauth.clients.google import GoogleOAuth2
|
||||||
from tortoise import fields
|
from tortoise import fields
|
||||||
from tortoise.contrib.starlette import register_tortoise
|
from tortoise.contrib.starlette import register_tortoise
|
||||||
|
from starlette.requests import Request
|
||||||
|
|
||||||
DATABASE_URL = "sqlite://./test.db"
|
DATABASE_URL = "sqlite://./test.db"
|
||||||
SECRET = "SECRET"
|
SECRET = "SECRET"
|
||||||
@ -59,10 +60,10 @@ app.include_router(google_oauth_router, prefix="/google-oauth", tags=["users"])
|
|||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_register()
|
@fastapi_users.on_after_register()
|
||||||
def on_after_register(user: User):
|
def on_after_register(user: User, request: Request):
|
||||||
print(f"User {user.id} has registered.")
|
print(f"User {user.id} has registered.")
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
@fastapi_users.on_after_forgot_password()
|
||||||
def on_after_forgot_password(user: User, token: str):
|
def on_after_forgot_password(user: User, token: str, request: Request):
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
||||||
|
@ -1,90 +0,0 @@
|
|||||||
import databases
|
|
||||||
import sqlalchemy
|
|
||||||
from fastapi import FastAPI
|
|
||||||
from fastapi_users import FastAPIUsers, models
|
|
||||||
from fastapi_users.authentication import JWTAuthentication
|
|
||||||
from fastapi_users.db import (
|
|
||||||
SQLAlchemyBaseOAuthAccountTable,
|
|
||||||
SQLAlchemyBaseUserTable,
|
|
||||||
SQLAlchemyUserDatabase,
|
|
||||||
)
|
|
||||||
from httpx_oauth.clients.google import GoogleOAuth2
|
|
||||||
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
|
|
||||||
|
|
||||||
DATABASE_URL = "sqlite:///./test.db"
|
|
||||||
SECRET = "SECRET"
|
|
||||||
|
|
||||||
|
|
||||||
google_oauth_client = GoogleOAuth2("CLIENT_ID", "CLIENT_SECRET")
|
|
||||||
|
|
||||||
|
|
||||||
class User(models.BaseUser, models.BaseOAuthAccountMixin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class UserCreate(User, models.BaseUserCreate):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class UserUpdate(User, models.BaseUserUpdate):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class UserDB(User, models.BaseUserDB):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
database = databases.Database(DATABASE_URL)
|
|
||||||
Base: DeclarativeMeta = declarative_base()
|
|
||||||
|
|
||||||
|
|
||||||
class UserTable(Base, SQLAlchemyBaseUserTable):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class OAuthAccount(SQLAlchemyBaseOAuthAccountTable, Base):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
engine = sqlalchemy.create_engine(
|
|
||||||
DATABASE_URL, connect_args={"check_same_thread": False}
|
|
||||||
)
|
|
||||||
Base.metadata.create_all(engine)
|
|
||||||
|
|
||||||
users = UserTable.__table__
|
|
||||||
oauth_accounts = OAuthAccount.__table__
|
|
||||||
user_db = SQLAlchemyUserDatabase(UserDB, database, users, oauth_accounts)
|
|
||||||
|
|
||||||
|
|
||||||
auth_backends = [
|
|
||||||
JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
|
|
||||||
]
|
|
||||||
|
|
||||||
app = FastAPI()
|
|
||||||
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):
|
|
||||||
print(f"User {user.id} has registered.")
|
|
||||||
|
|
||||||
|
|
||||||
@fastapi_users.on_after_forgot_password()
|
|
||||||
def on_after_forgot_password(user: User, token: str):
|
|
||||||
print(f"User {user.id} has forgot their password. Reset token: {token}")
|
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
|
||||||
async def startup():
|
|
||||||
await database.connect()
|
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
|
||||||
async def shutdown():
|
|
||||||
await database.disconnect()
|
|
Reference in New Issue
Block a user