psyf
01/13/2024, 2:04 AM@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 routerp_st
01/13/2024, 5:18 AMpsyf
01/13/2024, 5:19 AMuser
is using it because session.user_id == url.user_id, like you correctly point outpsyf
01/13/2024, 5:19 AMpsyf
01/13/2024, 5:23 AMrp_st
01/13/2024, 5:24 AMpsyf
01/13/2024, 5:25 AMpsyf
01/13/2024, 5:25 AMBut if you want to keep it, you can make your own middleware that does the above I guess.
rp_st
01/13/2024, 5:25 AMpsyf
01/13/2024, 5:28 AMrp_st
01/13/2024, 5:35 AM