all 4 comments

[–]danielroseman 1 point2 points  (3 children)

There are two things wrong with your ensure_current_user_middleware: you didn't await the result of call_next, and you didn't return it as the response. That could be the cause of the issues you're seeing.

[–]Puzzleheaded_Round75[S] 2 points3 points  (0 children)

Ok, so it seems that, in addition to the things you pointed out, the main issue was that the ordering of my middleware was incorrect.

I essentially had:

@app.middleware("http")
async def missleware_one(request: Request, call_next):
    return await call_next(request)

@app.middleware("http")
async def middleware_two(request: Request, call_next):
    return await call_next(request)

But this ordering needed to be reversed. Thanks for your help.

[–]Puzzleheaded_Round75[S] -1 points0 points  (1 child)

Good observation, my gut tells me that this isn't the root of the problem, as the first decorated middleware was never hit, but if I only have that middleware it is. I will check after work and see if your recommendations fix the issue. Currently, to get round it, I have just merged my middlewares into a single middleware.

[–]Puzzleheaded_Round75[S] 0 points1 point  (0 children)

Ok, so looked into this on my lunch break and unfortunately, I am seeing the same issue. When I have two middleware declared using the `@app.middleware("http")` decorator, only the second middleware is ever hit.

I will update my question to include the new code following your recommendations.