Currently I'm using JWT
token in django rest framework, while using login
api, i just get refresh token
& access token
. I'm wondring if could send the user id with these tokens too(i have no idea on if it's possible or not)
currently
{
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYyMjk3NzY2OCwianRpIjoiYWE2ZTk0NWNiYjRjNDAxZmFiMmM2NWEzZWQ1Yzg5NDUiLCJ1c2VyX2lkIjoxfQ.a54fcfa0ZsFrfVrb1VTdRO6bXY47NOuZqO8T1I3yKCc",
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjU0NDI3MjY4LCJqdGkiOiI1ODEwNDQyZWU3ZTM0MzczYTBkNmEzMDBkYmRmYTg2MyIsInVzZXJfaWQiOjF9.d6fmMq6ddsCaCyAEbDDaE5aja04LxYZmRP8WHfpmJqs"
}
what i want
{
"refresh": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYyMjk3NzY2OCwianRpIjoiYWE2ZTk0NWNiYjRjNDAxZmFiMmM2NWEzZWQ1Yzg5NDUiLCJ1c2VyX2lkIjoxfQ.a54fcfa0ZsFrfVrb1VTdRO6bXY47NOuZqO8T1I3yKCc",
"access": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjU0NDI3MjY4LCJqdGkiOiI1ODEwNDQyZWU3ZTM0MzczYTBkNmEzMDBkYmRmYTg2MyIsInVzZXJfaWQiOjF9.d6fmMq6ddsCaCyAEbDDaE5aja04LxYZmRP8WHfpmJqs",
"userid": <id>
}
if you using django-rest-framework-simplejwt, this can helpful. write your custom login function and add it in url path.
from rest_framework_simplejwt.tokens import RefreshToken
def get_tokens_for_user(user):
refresh = RefreshToken.for_user(user)
return {
'refresh': str(refresh),
'access': str(refresh.access_token),
'user': user
}
# in views.py
def login(request):
...
user = authenticate(email=email, password=password)
if user is not None:
user_id = User.objects.get(email=email)
data = get_tokens_for_user(user_id)
return Response(data, status=HTTP_200_OK)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.