I'm using the simpleJWT authentication in Django. By default the response is like this:
{ "refresh"=""
"access"= ""
}
I want to customize the response not to have a header and to contain some user details eg
{
username: ' ',
detail1: ' ',
detail2: ' ',
accessToken: ' ',
refreshToken: ' '
}
How can I implement a response like this using simpleJWT?
you can write your own view for the api if you want to customize, you don't need to use the existed one
to create the token from from the docs https://django-rest-framework-simplejwt.readthedocs.io/en/latest/creating_tokens_manually.html
serializer.py:
from rest_framework import serializers, viewsets, status
class SignInSerializer(serializers.Serializer):
username = serializers.CharField(max_length=255, required=True)
password = serializers.CharField(max_length=255, required=True, write_only=True)
views.py
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework.views import APIView
from django.http import JsonResponse
from django.contrib.auth import authenticate
class signin(APIView):
permission_classes = ()
authentication_classes = ()
def post(self, request):
received_json_data=request.data
serializer = SignInSerializer(data=received_json_data)
if serializer.is_valid():
user = authenticate(
request,
username=received_json_data['username'],
password=received_json_data['password'])
if user is not None:
refresh = RefreshToken.for_user(user)
return JsonResponse({
'refresh': str(refresh),
'access': str(refresh.access_token),
# your other stuffs <- add here
}, status=200)
else:
return JsonResponse({
'message': 'invalid username or password',
}, status=403)
else:
return JsonResponse({'message':serializer.errors}, status=400)