(HELP) FastAPI get current user - JWT Token #4161
-
|
So I am currently writing a User management API with FastAPI and certain endpoints are protected by this JWT Token. The JWT authorization is written as follows. I did not use the oauth2 method prescribed by FastAPI for certain reasons. I want to be able to extract the user email from the token, Is this possible? I did see an implementation of this in the FastAPI documentation - https://fastapi.tiangolo.com/tutorial/security/get-current-user/ however, me being new to FastAPI and stuff, I just cannot figure out how to implement it in my project. Can someone guide me through this? TIA! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
you could make dependency that take jwt and decode to get the claims/payload then return it async def get_current_user(token: str = Depends(JWTBearer())) -> dict:
# skipping verify since its already verified in JWTBearer
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM], verify_signature=False)
return {
"email": payload.get("email")
}you also could save the payloads in request.state while verifying in JWTBearer to skip redecoding the token |
Beta Was this translation helpful? Give feedback.
-
|
Is there a way to do this without using |
Beta Was this translation helpful? Give feedback.
you could make dependency that take jwt and decode to get the claims/payload then return it
you also could save the payloads in request.state while verifying in JWTBearer to skip redecoding the token