-
Hi there, I'm involved in developing an application which will have a private alpha phase. I would like to restrict registration to a specified list of email addresses. Does fastapi-users support this out of the box? If not, how might I extend it to do so? thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can either override the create method(https://github.com/fastapi-users/fastapi-users/blob/master/fastapi_users/manager.py#L110) from UserManager (https://github.com/fastapi-users/fastapi-users/blob/master/examples/sqlalchemy/app/users.py#L18): async def create(
self,
user_create: schemas.UC,
safe: bool = False,
request: Optional[Request] = None,
) -> models.UP:
"""
Create a user in the database.
Triggers the on_after_register handler on success.
:param user_create: The UserCreate model to create.
:param safe: If True, sensitive values like is_superuser or is_verified
will be ignored during the creation, defaults to False.
:param request: Optional FastAPI request that
triggered the operation, defaults to None.
:raises UserAlreadyExists: A user already exists with the same e-mail.
:return: A new user.
"""
# Add logic here to restrict specific email addresses.
return await super().create(user_create=user_create, safe=safe, request=request) Alternatively, you can perform validation using Pydantic: Pydantic v2: https://docs.pydantic.dev/2.4/concepts/validators/#field-validators https://github.com/fastapi-users/fastapi-users/blob/master/examples/sqlalchemy/app/schemas.py |
Beta Was this translation helpful? Give feedback.
You can either override the create method(https://github.com/fastapi-users/fastapi-users/blob/master/fastapi_users/manager.py#L110) from UserManager (https://github.com/fastapi-users/fastapi-users/blob/master/examples/sqlalchemy/app/users.py#L18):