Skip to content

FastAPIUser

Sample API User that gives basic functionality

Source code in fastapi_auth_middleware/middleware.py
class FastAPIUser(BaseUser):
    """ Sample API User that gives basic functionality """

    def __init__(self, first_name: str, last_name: str, user_id: any):
        """ FastAPIUser Constructor

        Args:
            first_name (str): The first name of the user
            last_name (str): The last name of the user
            user_id (any): The user id, most likely an integer or string
        """
        self.first_name = first_name
        self.last_name = last_name
        self.user_id = user_id

    @property
    def is_authenticated(self) -> bool:
        """ Checks if the user is authenticated. This method essentially does nothing, but it could implement session logic for example.

        Returns:
            bool: True if the user is authenticated
        """
        return True

    @property
    def display_name(self) -> str:
        """ Display name of the user """
        return f'{self.first_name} {self.last_name}'

    @property
    def identity(self) -> str:
        """ Identification attribute of the user """
        return self.user_id

display_name: str property readonly

Display name of the user

identity: str property readonly

Identification attribute of the user

is_authenticated: bool property readonly

Checks if the user is authenticated. This method essentially does nothing, but it could implement session logic for example.

Returns:

Type Description
bool

True if the user is authenticated

__init__(self, first_name, last_name, user_id) special

FastAPIUser Constructor

Parameters:

Name Type Description Default
first_name str

The first name of the user

required
last_name str

The last name of the user

required
user_id any

The user id, most likely an integer or string

required
Source code in fastapi_auth_middleware/middleware.py
def __init__(self, first_name: str, last_name: str, user_id: any):
    """ FastAPIUser Constructor

    Args:
        first_name (str): The first name of the user
        last_name (str): The last name of the user
        user_id (any): The user id, most likely an integer or string
    """
    self.first_name = first_name
    self.last_name = last_name
    self.user_id = user_id