Acquiring current user in middleware #1079
-
I was interested in providing API-wide middleware to attribute an IP to a user in the background. I intended to use middleware. Here's a slightly simplified version of what I've got right now. @app.middleware("http")
async def record_user_activity(request: Request, call_next):
ip = request.client.host
user = await current_user(redis='blah')
response = await call_next(request)
return response And the logged exception... ERROR: Exception in ASGI application
Traceback (most recent call last):
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 404, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\fastapi\applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\starlette\applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
raise exc
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\starlette\middleware\base.py", line 68, in __call__
response = await self.dispatch_func(request, call_next)
File "F:\Programming\Projects\motophoto\server\main.py", line 43, in record_user_activity
user = await current_user(redis=request.cookies)
File "<makefun-gen-7>", line 2, in current_user_dependency
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\fastapi_users\authentication\authenticator.py", line 136, in current_user_dependency
user, _ = await self._authenticate(
File "C:\Users\Xevion\.virtualenvs\motophoto-54a1Qt4r\lib\site-packages\fastapi_users\authentication\authenticator.py", line 170, in _authenticate
user = await strategy.read_token(token, user_manager)
AttributeError: 'Depends' object has no attribute 'read_token' |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The If you really need to retrieve the user in the middleware, you'll need to manually make the operations the DI system normally do:
|
Beta Was this translation helpful? Give feedback.
-
I think it would be really useful to have an off the shelf middleware that would either return attach current user to the request or |
Beta Was this translation helpful? Give feedback.
The
current_user
function is designed to work inside the dependency injection framework of FastAPI. It won't work outside it.If you really need to retrieve the user in the middleware, you'll need to manually make the operations the DI system normally do:
UserManager
manually, taking care of passing all needed arguments (see: https://fastapi-users.github.io/fastapi-users/10.1/cookbook/create-user-programmatically/#create-a-user-programmatically)read_token
method with the token you retrieved