Skip to content

Acquiring current user in middleware #1079

Answered by frankie567
Xevion asked this question in Q&A
Discussion options

You must be logged in to vote

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:

  1. Retrieve the token manually from the request, whether it's a cookie, an authorization header or whatever
  2. Instantiate the 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)
  3. Call the read_token method with the token you retrieved

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@Xevion
Comment options

@frankie567
Comment options

@Xevion
Comment options

Answer selected by Xevion
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants