* Revamp authentication to allow multiple backends * Make router generate a login route for each backend * Apply black * Remove unused imports * Complete docstrings * Update documentation * WIP add cookie auth * Complete cookie auth unit tests * Add documentation for cookie auth * Fix cookie backend default name * Don't make cookie return a Response
2.3 KiB
Router
We're almost there! The last step is to configure the FastAPIUsers object that will wire the database adapter, the authentication class and the user model to expose the FastAPI router.
Configure FastAPIUsers
Configure FastAPIUsers object with all the elements we defined before. More precisely:
db: Database adapter instance.auth_backends: List of authentication backends. See Authentication.user_model: Pydantic model of a user.reset_password_token_secret: Secret to encode reset password token.reset_password_token_lifetime_seconds: Lifetime of reset password token in seconds. Default to one hour.
from fastapi_users import FastAPIUsers
fastapi_users = FastAPIUsers(
user_db,
auth_backends,
User,
SECRET,
)
And then, include the router in the FastAPI app:
app = FastAPI()
app.include_router(fastapi_users.router, prefix="/users", tags=["users"])
Event handlers
In order to be as unopinionated as possible, we expose decorators that allow you to plug your own logic after some actions. You can have several handlers per event.
After register
This event handler is called after a successful registration. It is called with one argument: the user that has just registered.
Typically, you'll want to send a welcome e-mail or add it to your marketing analytics pipeline.
You can define it as an async or standard method.
Example:
@fastapi_users.on_after_register()
def on_after_register(user: User):
print(f"User {user.id} has registered.")
After forgot password
This event handler is called after a successful forgot password request. It is called with two arguments: the user which has requested to reset their password and a ready-to-use JWT token that will be accepted by the reset password route.
Typically, you'll want to send an e-mail with the link (and the token) that allows the user to reset their password.
You can define it as an async or standard method.
Example:
@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}")
Next steps
Check out a full example that will show you the big picture.