TLDR - But by default the JWT token sent from the backend only includes the username. I want the userId as well.This is what it contains currently:-
HEADER:ALGORITHM & TOKEN TYPE
{
"typ": "JWT",
"alg": "HS256"
}
PAYLOAD:DATA
{
"username": "admin", <---- I have the username already. Need the userId as well!
"exp": 1621845856,
"origIat": 1621845556
}
VERIFY SIGNATURE
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
your-256-bit-secret
) secret base64 encoded
I have a Django GraphQL API (using Graphene, django-graphql-auth, django-graphql-jwt) and an Angular frontend. I have authentication setup fully. Now the issue I'm facing is that I'm storing the token in the localstorage and so when the user opens the browser again, I want to verify that token and use just the information in there to obtain the user's id and then use that userId to fetch their user profile information, which is an essential step to complete the login process.
I am not sure how to customize the payload to include the userId as well. So that when the user opens the browser after shutting down, I can simply use just the information in the token to fully log them in. I'm very new to both Python and Django. I couldn't find the process detailed anywhere online, maybe I wasn't looking for the right thing or in the right place. Detailed instructions would be very helpful.
The following is a more thorough reproduction of solution found here.
We basically need to override the jwt_payload method that comes as part of the
graphql_jwt
package.Add this to project's
settings.py
Create the following files in a folder
common
in the same directory where you have themanage.py
file.Inside, create the following two files:- Here's where we're adding the user_id in the
sub
attribute.utils.py
This settings.py file inside the common folder is just a dependency. for the utils.py.
settings.py