1

I am using allauth for authentication in drf. I was able to register new users and login with the credentials. The login api return response something like this:

{
 "key" : "<some token>"
}

Now I have 1 more API the code for which is

    from django.http import HttpResponse
    def lol(request):
        if request.user.is_authenticated:
            return HttpResponse("Authenticated")
        else:
            return HttpResponse("Not Authenticated")

But this is always returning not authenticated my api call look like thisenter image description here

Here is the list of installed apps in my settings.py

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',

'api.user',
'api.activity',
]

1 Answer 1

3

It seems you're using functional views? If so did you add @api_view decorator to the view?

If so did you add authentication_classes=[TokenAuthentication] keyword argument? That's imperative to make token Auth work.

Either that or set the following in settings.py:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication'
    ]
}
1
  • Thanks a lot! I was missing @api_view. Nov 13, 2018 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.