Hello friends!
All my api route basically look like this (fastAPI):
@app.get("/api/users/{user_id}/recommendations")
def getRecommendations(
user_id: str,
session: SessionContainer = Depends(verify_session()),
):
if user_id != session.get_user_id():
return status.HTTP_401_UNAUTHORIZED
...
Is there a better way to make sure only
session.get_user_id() == {user_id}
else
401
?
Why do I want this?
- Gets cumbersome and error-prone to add this at the end of every route
- When I add more roles into the system, the initial check becomes fairly larger than just 1
if
statement, and copy-pasting it everywhere feels like a giant red flag
My plan if there's no better "supertokens way" of doing things:
- define function
def is_authorized(session: SessionContainer, request: Request) -> bool:
- call it on the first line of anuy API route