I am a novice in Django and have started learning about jwt authentication implementation in django. I have successfully obtained access token and refresh token on successful login (username and password) by a user. Now, if I want to get the same user details like email, username and his mobile number when he clicks on some button from frontend, how to do it? Does the returned token from backend gets stored in frontend so that I can send it in the fetch function OR is there any way to get access to that token in another APIView in backend? I have tried every code provided in the answers for the similar question but not getting the result. Please help me in understanding this and as well as the code to implement it.
you have many ways to do so, one of them is to read the user's data directly from the database based on the authenticated user. so the view would be:
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def user_view(request: HttpRequest):
if (request.method == 'GET'):
user = User.objects.get(id=request.user.id)
serialized_user = UserSerializer(user)
return Response(serialized_user.data)
and I used a serializer since it's always a good idea to User serializers:
from rest_framework import serializers
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
and now add the view to your user patterns like so for example:
path('user-data/', views.user_view)
and now to test you add the access token to your bearer data like so: