Django custom authentication

77 views
Skip to first unread message

Praveen Madhavan

unread,
Oct 22, 2013, 7:53:07 AM10/22/13
to django...@googlegroups.com
Hello All,

     I am trying custom authentication with django, I wrote a class and filled it with the methods authenticate and get_user, I also added this authentication to the AUTHENTICATION_BACKENDS in settings.py file.

    I have called my custom authenticate method and followed it up with login in my view.

    Everything seems to work fine, is_authenticated returns true for the user after login, but the subsequent requests have request.user as anonymous, unable to figure out the reason, require your help


Thanks
Praveen.M

François Schiettecatte

unread,
Oct 22, 2013, 8:13:27 AM10/22/13
to django...@googlegroups.com
Praveen

I would check that cookies are being returned from the browser, the session information in stored in them, and did you check that get_user() is returning the user corresponding to the ID?

François
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b2da7920-4154-4315-b4e7-957c869aa727%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Tom Evans

unread,
Oct 22, 2013, 9:00:13 AM10/22/13
to django...@googlegroups.com
On Tue, Oct 22, 2013 at 12:53 PM, Praveen Madhavan
<prave...@gmail.com> wrote:
> Hello All,
>
> I am trying custom authentication with django, I wrote a class and
> filled it with the methods authenticate and get_user, I also added this
> authentication to the AUTHENTICATION_BACKENDS in settings.py file.
>
> I have called my custom authenticate method and followed it up with
> login in my view.

Show us this view. You should not be calling a "custom authenticate
method", you should be using login() and authenticate() from
django.contrib.auth. If you are not, then this explains why on
subsequent views you are not logged in.

>
> Everything seems to work fine, is_authenticated returns true for the
> user after login, but the subsequent requests have request.user as
> anonymous, unable to figure out the reason, require your help

The other cause of login failing is if your browser does not send the
session cookie back to the server. This would happen if you have
configured django to send cookies with a different host than the pages
are served from. Use chrome inspector or firefox or any other tool you
fancy to determine if this is the case.

The easiest way to see is to look at the session cookie sent with the
pre-login page response, and the session cookie sent with the
post-login page response, do they have different ids?

Cheers

Tom

Praveen Madhavan

unread,
Oct 22, 2013, 11:14:16 AM10/22/13
to django...@googlegroups.com, teva...@googlemail.com
Tom

Thanks for the response, here is my view

def home(request):
    if not request.user.is_authenticated():
        # I have written my own custom authenticate method that returns an user object
        user=authenticate(request=request)
        if not user:
            return HttpResponseRedirect("/accounts")
        else:
            login(request,user)
            return HttpResponse("Logged in Successfully")
    else:
        return HttpResponse(request.user)

Praveen Madhavan

unread,
Oct 24, 2013, 12:47:03 PM10/24/13
to django...@googlegroups.com, teva...@googlemail.com
I checked for the session cookie...
It is getting created, could it be the django version ? I am using django 1.5 and I found few django openid apps not working on 1.5 .
Reply all
Reply to author
Forward
0 new messages