Don't set Pydantic schemas on FastAPIUsers class: pass it directly on router creation

This commit is contained in:
François Voron
2022-05-04 11:25:39 +02:00
parent a90924b707
commit 940b2091b1
3 changed files with 35 additions and 40 deletions

View File

@@ -22,54 +22,49 @@ except ModuleNotFoundError: # pragma: no cover
BaseOAuth2 = Type # type: ignore
class FastAPIUsers(Generic[models.UP, models.ID, schemas.U, schemas.UC, schemas.UU]):
class FastAPIUsers(Generic[models.UP, models.ID]):
"""
Main object that ties together the component for users authentication.
:param get_user_manager: Dependency callable getter to inject the
user manager class instance.
:param auth_backends: List of authentication backends.
:param user_schema: Pydantic schema of a public user.
:param user_create_schema: Pydantic schema for creating a user.
:param user_update_schema: Pydantic schema for updating a user.
:attribute current_user: Dependency callable getter to inject authenticated user
with a specific set of parameters.
"""
authenticator: Authenticator
_user_schema: Type[schemas.U]
_user_create_schema: Type[schemas.UC]
_user_update_schema: Type[schemas.UU]
def __init__(
self,
get_user_manager: UserManagerDependency[models.UP, models.ID],
auth_backends: Sequence[AuthenticationBackend],
user_schema: Type[schemas.U],
user_create_schema: Type[schemas.UC],
user_update_schema: Type[schemas.UU],
):
self.authenticator = Authenticator(auth_backends, get_user_manager)
self._user_schema = user_schema
self._user_create_schema = user_create_schema
self._user_update_schema = user_update_schema
self.get_user_manager = get_user_manager
self.current_user = self.authenticator.current_user
def get_register_router(self) -> APIRouter:
"""Return a router with a register route."""
def get_register_router(
self, user_schema: Type[schemas.U], user_create_schema: Type[schemas.UC]
) -> APIRouter:
"""
Return a router with a register route.
:param user_schema: Pydantic schema of a public user.
:param user_create_schema: Pydantic schema for creating a user.
"""
return get_register_router(
self.get_user_manager,
self._user_schema,
self._user_create_schema,
self.get_user_manager, user_schema, user_create_schema
)
def get_verify_router(self) -> APIRouter:
"""Return a router with e-mail verification routes."""
return get_verify_router(self.get_user_manager, self._user_schema)
def get_verify_router(self, user_schema: Type[schemas.U]) -> APIRouter:
"""
Return a router with e-mail verification routes.
:param user_schema: Pydantic schema of a public user.
"""
return get_verify_router(self.get_user_manager, user_schema)
def get_reset_password_router(self) -> APIRouter:
"""Return a reset password process router."""
@@ -118,18 +113,22 @@ class FastAPIUsers(Generic[models.UP, models.ID, schemas.U, schemas.UC, schemas.
def get_users_router(
self,
user_schema: Type[schemas.U],
user_update_schema: Type[schemas.UU],
requires_verification: bool = False,
) -> APIRouter:
"""
Return a router with routes to manage users.
:param user_schema: Pydantic schema of a public user.
:param user_update_schema: Pydantic schema for updating a user.
:param requires_verification: Whether the endpoints
require the users to be verified or not.
"""
return get_users_router(
self.get_user_manager,
self._user_schema,
self._user_update_schema,
user_schema,
user_update_schema,
self.authenticator,
requires_verification,
)