Django not authenticating access token

I am using JWT to authenticate users, on hitting login endpoint, I am getting the following output in my Postman

{
    "message": "Login successful",
    "user_id": 2,
    "username": "Animesh0764",
    "tokens": {
        "refresh": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTczNjU5MTAxNywiaWF0IjoxNzM1Mjk1MDE3LCJqdGkiOiI4YmJhMTczZmZkNDg0OWIzODU3YTZkMDE1MDZlNzM2ZCIsInVzZXJfaWQiOjJ9.-wX6S9yxNgFCjIR8Tu0FRc-Q2ivFDMVJouXkKkjDNtI",
        "access": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzM1Mjk1OTE3LCJpYXQiOjE3MzUyOTUwMTcsImp0aSI6IjdhOWU3ZDA0NTllNzQxMzFhNDM1MWVlZjVkOWRiODcyIiwidXNlcl9pZCI6Mn0.LI6afRT66MFWTpomo1E9BHn5JTrRuGoTeX0EEwMRFvQ"
    }
}

While using this access token in Bearer for Profile view, it shows:

{
    "detail": "User not found",
    "code": "user_not_found"
}

Visited jwt.io and verified my token, everything seems okay, it shows the correct user_id but still won't verify. The views for both are given below

#Login user
class UserLoginView(APIView):
    def post(self, req):
        serializer = UserLoginSerializer(data=req.data)
        if serializer.is_valid(raise_exception=True):
            user = serializer.validated_data['user']
            return Response({
                "message": "Login successful",
                "user_id": user.id,
                "username": user.username,
                "tokens": get_tokens_for_user(user)
            }, status=HTTP_200_OK)
        return Response(
            {"message": "Login failed"},
            status=HTTP_400_BAD_REQUEST
        )

#View user profile
class UserProfileView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, req):
        print(f"Request user: {req.user}")
        print(f"User authenticated: {req.user.is_authenticated}")
        if req.user.is_authenticated:
            user = req.user
            serializer = UserProfileSerializer(user)
            return Response(serializer.data, status=HTTP_200_OK)
        return Response({"message": "Unauthorized"}, status=HTTP_401_UNAUTHORIZED)

It should verify the token and then show the response 200 but always shows 401 Unauthorized

Even added everything required in settings.py file

can you provide all the code setting.py, view.py and urls.py

Вернуться на верх