0

I am using JWT auth to login users. Username and password are sent in Body, however, in the customized response, an anonymUser is always returned. I think the problem is that in settings.py stands 'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION', and when I generate a token before and then send it in Headers the user is identified. Bit the thing is, that I cannot use 2 views in order to generate token and decode it, everything has to be in one view and I don't know how to login the user in the view and then get token and decode it.

@api_view(('POST',))
def check_token(request):
    token_refresh = RefreshToken.for_user(request.user)
    print(request.user) # AnonymUser
    print(request.user.id) # None

    print(str(token_refresh.access_token))
    data = {'token': str(token_refresh.access_token), 'refresh_token': str(token_refresh)}
    aT = str.encode(str(token_refresh.access_token))


    try:
        valid_data = TokenBackend(algorithm='HS256').decode(aT, verify=False)
        print(valid_data)
        data['uuid'] = valid_data['user_id']
        data['validUntil'] = valid_data['exp']
        data['clientId'] = 'default'
        return JsonResponse(data)
    except ValidationError as v:
        print("Validation error", v)


Anna
  • 914
  • 9
  • 25

1 Answers1

0

The answer can be found here

The The request.user is set by the django.contrib.auth.middleware.AuthenticationMiddleware.

So the request.user does not know about JWT as it is using Djangos authentication system. You can read about using JWT with Django here

Helge Schneider
  • 483
  • 5
  • 8