Accessing the user object
You can access your user object in two ways, either with Depends(<schema name>)
or with request.state.user
.
Depends(<schema name>)
- Python 3.9 or above
- Python 3.8
depends_api_example.py
from fastapi import APIRouter, Depends
from demo_project.api.dependencies import azure_scheme
from fastapi_azure_auth.user import User
router = APIRouter()
@router.get(
'/hello-user',
response_model=User,
operation_id='helloWorldApiKey',
)
async def hello_user(user: User = Depends(azure_scheme)) -> dict[str, bool]:
"""
Wonder how this auth is done?
"""
return user.dict()
depends_api_example.py
from fastapi import APIRouter, Depends
from typing import Dict
from demo_project.api.dependencies import azure_scheme
from fastapi_azure_auth.user import User
router = APIRouter()
@router.get(
'/hello-user',
response_model=User,
operation_id='helloWorldApiKey',
)
async def hello_user(user: User = Depends(azure_scheme)) -> Dict[str, bool]:
"""
Wonder how this auth is done?
"""
return user.dict()
request.state.user
- Python 3.9 or above
- Python 3.8
request_state_user_api_example.py
from fastapi import APIRouter, Depends, Request
from demo_project.api.dependencies import azure_scheme
from fastapi_azure_auth.user import User
router = APIRouter()
@router.get(
'/hello-user',
response_model=User,
operation_id='helloWorldApiKey',
dependencies=[Depends(azure_scheme)]
)
async def hello_user(request: Request) -> dict[str, bool]:
"""
Wonder how this auth is done?
"""
return request.state.user.dict()
request_state_user_api_example.py
from fastapi import APIRouter, Depends, Request
from typing import Dict
from demo_project.api.dependencies import azure_scheme
from fastapi_azure_auth.user import User
router = APIRouter()
@router.get(
'/hello-user',
response_model=User,
operation_id='helloWorldApiKey',
dependencies=[Depends(azure_scheme)]
)
async def hello_user(request: Request) -> Dict[str, bool]:
"""
Wonder how this auth is done?
"""
return request.state.user.dict()