routers

get_users_router

msdss_users_api.routers.get_users_router(fastapi_users_objects=None, route_settings={'cookie': {'_enable': True, '_get_user': None, 'prefix': '/auth', 'tags': ['auth']}, 'jwt': {'_enable': True, '_enable_refresh': True, '_get_user': None, 'prefix': '/auth/jwt', 'tags': ['auth']}, 'register': {'_enable': True, '_get_user': {'superuser': True}, 'prefix': '/auth', 'tags': ['auth']}, 'reset': {'_enable': True, '_get_user': None, 'prefix': '/auth', 'tags': ['auth']}, 'users': {'_enable': True, '_get_user': None, 'prefix': '/users', 'tags': ['users']}, 'verify': {'_enable': True, '_get_user': None, 'prefix': '/auth', 'tags': ['auth']}}, *args, **kwargs)[source]

Get a users router.

Parameters
  • fastapi_users_objects (dict) – Dictionary returned from msdss_users_api.tools.create_fastapi_users_objects().

  • route_settings (dict) –

    Dictionary of settings for the users routes. Each route consists of the following keys:

    • path: resource path for the route

    • tags: tags for open api spec

    • _enable (bool): Whether this route should be included or not

    • _get_user (dict or None): Additional arguments passed to the msdss_users_api.msdss_users_api.core.UsersAPI.get_current_user() function for the route - if None, a dependency will not be added

    • _enable_refresh (bool): Only applies to ``jwt route - whether to include a jwt refresh route or not

    • **kwargs: Additional arguments passed to the fastapi:fastapi.FastAPI.include_router() method for this route

    The default settings are:

    {'cookie': {'_enable': True,
                '_get_user': None,
                'prefix': '/auth',
                'tags': ['auth']},
     'jwt': {'_enable': True,
             '_enable_refresh': True,
             '_get_user': None,
             'prefix': '/auth/jwt',
             'tags': ['auth']},
     'register': {'_enable': True,
                  '_get_user': {'superuser': True},
                  'prefix': '/auth',
                  'tags': ['auth']},
     'reset': {'_enable': True,
               '_get_user': None,
               'prefix': '/auth',
               'tags': ['auth']},
     'users': {'_enable': True,
               '_get_user': None,
               'prefix': '/users',
               'tags': ['users']},
     'verify': {'_enable': True,
                '_get_user': None,
                'prefix': '/auth',
                'tags': ['auth']}}
    

    Any unspecified settings will be replaced by their defaults.

  • *args – Additional arguments passed to fastapi:fastapi.routing.APIRouter.

  • **kwargs – Additional arguments passed to fastapi:fastapi.routing.APIRouter.

Returns

A router object used users routes. See FastAPI bigger apps.

Return type

fastapi:fastapi.routing.APIRouter

Author

Richard Wen <rrwen.dev@gmail.com>

Example

from msdss_base_api import API
from msdss_users_api.tools import create_fastapi_users_objects
from msdss_users_api.routers import get_users_router

# Create an app
app = API()

# Setup user manager secrets
user_manager_settings = dict(
    reset_password_token_secret='reset-secret', # CHANGE TO STRONG PHRASE
    verification_token_secret='verify-secret' # CHANGE TO STRONG PHRASE
)

# Setup jwt and cookie secret
jwt_settings = dict(secret='jwt-secret') # CHANGE TO STRONG PHRASE
cookie_settings = dict(secret='cookie-secret') # CHANGE TO STRONG PHRASE

# Create FastAPI Users objects
fastapi_users_objects = create_fastapi_users_objects(
    user_manager_settings=user_manager_settings,
    jwt_settings=jwt_settings,
    cookie_settings=cookie_settings
)

# Add the users router
router = get_users_router(fastapi_users_objects)
app.add_router(router)

# Setup startup and shutdown
async_database = fastapi_users_objects['databases']['async_database']

@app.event('startup')
async def startup():
    await async_database.connect()

@app.event('shutdown')
async def shutdown():
    await async_database.disconnect()

# Host app at https://localhost:8000
# Try it at https://localhost:8000/docs
# app.start()