1

after logging in the user and checking whether request.user isauthenticated in other activity in android. the value is always false.

the following code is used for login a user

from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,logout,login


@api_view(['POST'])
def userRegister(request):
    user=User.objects.create_user(username=request.POST['username'],email=request.POST['email'],password=request.POST['password'])
    return Response({'ok':'True'},status=status.HTTP_201_CREATED)

@api_view(['POST'])
def userLogin(request):
    user=authenticate(
        username=request.POST['username'],
        password=request.POST['password']
    )
    if user is not None:
        login(request,user)
        return Response({'ok':'True'},status=status.HTTP_200_OK)
    else:
        return Response({'ok':'False'},status=status.HTTP_401_UNAUTHORIZED)

the following code is used to check whether the user is authenticated or not

from rest_framework.response import Response
from rest_framework.decorators import api_view
from . import models
from . import serializers
from django.contrib.auth.models import User
from rest_framework import status


@api_view(['GET'])
def HomeView(request):
    if request.user.is_authenticated:
        return Response(data={"ok":'true'})
    else:
        return Response(data={"ok":"false"})
sabari rangan
  • 608
  • 1
  • 6
  • 14

1 Answers1

0

In your view (if you are using a version of Django < 1.10, as @Kevin Christopher Henry points out in the comments below) you need to do:

if request.user.is_authenticated():

If you do .is_authenticated (without the parentheses) it will return the method itself instead of a boolean (True or False), and will always evaluate to True, or at least it should. I am not sure why it would return False in your case, but I think this should solve your problem.

Also, note that in Django templates the situation is different. In a template you can (and must) do:

{% if user.is_authenticated %} 

Note the lack of parentheses after is_authenticated. If you include parentheses in the template case, you will get an error, while if you don't include them in the view case you will get unexpected results.

elethan
  • 15,400
  • 8
  • 58
  • 83
  • 1
    FYI, in Django 1.10 [`is_authenticated` was turned into a property](https://docs.djangoproject.com/en/1.10/ref/contrib/auth/#django.contrib.auth.models.User.is_authenticated) for the exact purpose of avoiding the kind of bug you describe. – Kevin Christopher Henry Sep 04 '16 at 16:28
  • @KevinChristopherHenry very interesting. Thank you for this valuable piece of information. I do not have any experience with the most recent version of Django, so perhaps this user's issue lies elsewhere. – elethan Sep 04 '16 at 16:33
  • @KevinChristopherHenry then how do you check if a user is authenticated in 1.10 ? – sidpat Jan 13 '17 at 10:37
  • @sidpat it depends on the context, but until version 2.0 the old method of doing this will continue to be supported for backwards compatibility. The new way to do this will be to use `is_authenticated` instead of `is_authenticated()`. – elethan Jan 13 '17 at 13:00
  • @elathan you mean `requests.user.is_authenticated` ? – sidpat Jan 13 '17 at 13:16
  • @sidpat Yes. See the link in the first comment above. – elethan Jan 13 '17 at 13:18