how to debug the endpoints created by fastapi_users.get_users_router #1271
-
class GlobalUserMiddleware:
def __init__(self, app: ASGIApp) -> None:
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
from src.lib.logger import log
if scope["type"] != "http":
await self.app(scope, receive, send)
return
if scope["type"] == "http":
request = Request(scope)
try:
subject = current_user().id
except Exception as e:
log.error(e)
subject = None
global_userid.set(subject)
await self.app(scope, receive, send)
|
Beta Was this translation helpful? Give feedback.
Answered by
frankie567
Aug 17, 2023
Replies: 1 comment 3 replies
-
The async def set_global_user(request: Request, user = Depends(current_user)):
# request.state is really useful to put shared state in the request object
# Ref: https://www.starlette.io/requests/#other-state
request.state.global_user_id = user.id
app = FastAPI(dependencies=[Depends(set_global_user)]) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the
optional
flag in this case: