Yash Thenuan
Yash Thenuan

Reputation: 631

request.user.is_authenticated is always false in Django rest framework

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',
]

Upvotes: 1

Views: 3003

Answers (1)

Rich Tier
Rich Tier

Reputation: 9461

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'
    ]
}

Upvotes: 3

Related Questions