Skip to content

Users

users

User management core logic.

Provides business logic for user creation and authentication, independent of the database implementation.

Classes

UserAlreadyExistsError

Bases: Exception

Raised when trying to create a user with an existing email.

InvalidCredentialsError

Bases: Exception

Raised when login credentials are invalid.

EmailNotVerifiedError

Bases: Exception

Raised when email is not verified

Functions

create_user

create_user(*, users: UserAdapter, email: str, password: str) -> Any

Create a new user with a hashed password.

Parameters:

Name Type Description Default
users UserAdapter

User adapter for database operations

required
email str

User's email address

required
password str

Plain text password (will be hashed)

required

Returns:

Type Description
Any

Created user object

Raises:

Type Description
UserAlreadyExistsError

If a user with the email already exists

Source code in fastauth/core/users.py
def create_user(
    *,
    users: UserAdapter,
    email: str,
    password: str,
) -> Any:
    """
    Create a new user with a hashed password.

    Args:
        users: User adapter for database operations
        email: User's email address
        password: Plain text password (will be hashed)

    Returns:
        Created user object

    Raises:
        UserAlreadyExistsError: If a user with the email already exists
    """

    existing_user = users.get_by_email(email=email)

    if existing_user:
        raise UserAlreadyExistsError(f"User with email {email} already exists")

    user = users.create_user(email=email, hashed_password=hash_password(password))

    return user

authenticate_user

authenticate_user(*, users: UserAdapter, email: str, password: str) -> Any

Authenticate a user by email and password.

Parameters:

Name Type Description Default
users UserAdapter

User adapter for database operations

required
email str

User's email address

required
password str

Plain text password to verify

required

Returns:

Type Description
Any

Authenticated user object

Raises:

Type Description
InvalidCredentialsError

If email doesn't exist, password is wrong, or user is inactive

EmailNotVerifiedError

If email verification is required but not completed

Source code in fastauth/core/users.py
def authenticate_user(
    *,
    users: UserAdapter,
    email: str,
    password: str,
) -> Any:
    """
    Authenticate a user by email and password.

    Args:
        users: User adapter for database operations
        email: User's email address
        password: Plain text password to verify

    Returns:
        Authenticated user object

    Raises:
        InvalidCredentialsError: If email doesn't exist, password is wrong, \
            or user is inactive
        EmailNotVerifiedError: If email verification is required but not completed
    """

    user = users.get_by_email(email=email)

    if not user:
        raise InvalidCredentialsError("Invalid email or password")

    if not verify_password(user.hashed_password, password):
        raise InvalidCredentialsError

    if settings.require_email_verification and not user.is_verified:
        raise EmailNotVerifiedError

    if not user.is_active:
        raise InvalidCredentialsError("User is inactive")

    return user