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

First of all, take a look at your settings and go into this

` section.AUTHENTICATION_BACKENDS = [

'django.contrib.auth.backends.ModelBackend'

] ` And what to do? Does this section exist or not? If so, what is its order? Whatever order it is set in, don't tell it to authenticate, it will proceed in the order it is in this list. Now, if you want to use a custom authentication system, we need to add it. For example, create an authentication section in the application for card logic and put it before your authentication system, like this:

` AUTHENTICATION_BACKENDS = [

'authenticate.backends.OTPBackend',
'django.contrib.auth.backends.ModelBackend'

] ` forexample you can use this code

` class OTPBackend(BaseBackend):

def authenticate(self, request, username=None, password=None, **kwargs):
    otp = kwargs.get('otp')
    phone_number = kwargs.get('phone_number')

`

Back to Top