Using fastapi, I’m struggling with adding “login_required”(or smh) middleware.
(I think middleware is the best idea, and if I dont get normal solution for my problem, I will have to work with decorators, or some hardcoding like putting hassatr(request.state, “user”) in each route)
Of course, I want to keep some pages free for unathorized people,
so my idea was to check request’s path if it’s in AUTH_PATHS list.
that was working till I’ve understood that I always have some flexible paths, like
@router.get('/posts/all/{page:int}')
If you know how is it can be done on real prod projects, I’d took that as answer
I’ve got 4 ideas:
-
Searching for some magic python/fastapi syntax I didn’t know (haven’t found, but maybe it is)
-
Using some python modules, like fastapi in-built pydantic (which has been made for validation, so it didn’t work)
-
Making something crazy, like everytime my database gets bigger and needs more pages to show all posts, it adds 1 to some value in config.py, and updates my settings class in config.py, including every single path there (like ‘/posts/all/154’, ‘/posts/all/155’) etc. (this is pure madness and I believe this would be very unefficient and slow)
-
Checking which endpoint (function) was called by request. Got this as the best idea (when I was already emptied), but then I couldn’t find any information if this is even real.