core

UsersAPI

class msdss_users_api.core.UsersAPI(cookie_secret=None, jwt_secret=None, reset_password_token_secret=None, verification_token_secret=None, cookie_lifetime=2592000, jwt_lifetime=900, database=<msdss_base_database.core.Database object>, users_router_settings={}, load_env=True, env=<msdss_users_api.env.UsersDotEnv object>, api=<fastapi.applications.FastAPI object>, *args, **kwargs)[source]

Users API class for managing users.

Parameters
  • cookie_secret (str or None) – A secret for cookie encryption. Use a strong phrase (e.g. openssl rand -hex 32). If None, the value will be taken from the environment variables. See parameter env.

  • jwt_secret (str or None) – A secret for JWT encryption. Use a strong phrase (e.g. openssl rand -hex 32). If None, the value will be taken from the environment variables. See parameter env.

  • reset_password_token_secret (str or None) – Secret used to secure password reset tokens. Use a strong phrase (e.g. openssl rand -hex 32). If None, the value will be taken from the environment variables. See parameter env.

  • verification_token_secret (str or None) – Secret used to secure verification tokens. Use a strong phrase (e.g. openssl rand -hex 32). If None, the value will be taken from the environment variables. See parameter env.

  • jwt_lifetime (int) – Expiry time of JSON Web Tokens (JWTs) in seconds.

  • cookie_lifetime (int) – Expiry time of cookies in seconds.

  • database (msdss_base_database.core.Database) – Database to use for managing users.

  • users_router_settings (dict) – Keyword arguments passed to msdss_users_api.routers.get_users_router() except fastapi_users_objects.

  • load_env (bool) – Whether to load variables from a file with environmental variables at env_file or not.

  • env (msdss_users_api.env.UsersDotEnv) –

    An object to set environment variables related to users configuration. These environment variables will overwrite the parameters above if they exist.

    By default, the related parameters above are assigned to each of the environment variables seen below if load_env is True:

    <parameter> = <environment variable>
    
    cookie_secret = MSDSS_USERS_COOKIE_SECRET
    jwt_secret = MSDSS_USERS_JWT_SECRET
    reset_password_token_secret = MSDSS_USERS_RESET_PASSWORD_TOKEN_SECRET
    verification_token_secret = MSDSS_USERS_VERIFICATION_TOKEN_SECRET
    

  • api (fastapi:fastapi.FastAPI) – API object for creating routes.

  • *args – Additional arguments passed to msdss_base_api.core.API.

  • **kwargs – Additional arguments passed to msdss_base_api.core.API.

users_api_database

Database object for users API.

Type

msdss_base_database.core.Database

misc

Dictionary of miscellaneous values:

Type

dict

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_users_api import UsersAPI

# Create users api app
app = UsersAPI(
    cookie_secret='cookie-secret', # CHANGE TO STRONG PHRASE
    jwt_secret='jwt-secret', # CHANGE TO STRONG PHRASE
    reset_password_token_secret='reset-secret', # CHANGE TO STRONG PHRASE
    verification_token_secret='verification-secret' # CHANGE TO STRONG PHRASE
)

# Run the app with app.start()
# Try API at http://localhost:8000/docs
# app.start()

get_current_user

UsersAPI.get_current_user(*args, **kwargs)[source]

Get a dependency function to retrieve the current authenticated user.

Parameters
  • *args – Additional arguments passed to fastapi_users:fastapi_users.FastAPIUsers.current_user(). See current_user.

  • **kwargs

    Additional arguments passed to fastapi_users:fastapi_users.FastAPIUsers.current_user(). See current_user.

Returns

A function to retrieve the current authenticated user. Useful for adding protected routes accessible only by authenticated users.

Return type

func

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from fastapi import Depends
from msdss_users_api import UsersAPI
from msdss_users_api.models import User

# Create users api app
app = UsersAPI(
    cookie_secret='cookie-secret', # CHANGE TO STRONG PHRASE
    jwt_secret='jwt-secret', # CHANGE TO STRONG PHRASE
    reset_password_token_secret='reset-secret', # CHANGE TO STRONG PHRASE
    verification_token_secret='verification-secret' # CHANGE TO STRONG PHRASE
)

# Get a function dependency for the current active user
current_active_user = app.get_current_user(active=True)

# Add a protected route
@app.route('GET', '/protected-route')
def protected_route(user: User = Depends(current_active_user)):
    return f'Hello, {user.email}'

# Run the app with app.start()
# Try API at http://localhost:8000/docs
# app.start()