I faced a similar issue (request.user becoming anonymous after login) and 
found
the following solution.  It had to do with the naming convention for the
auth backend under AUTHENTICATION_BACKENDS in settings.py.  I had a custom
auth backend class called PamBackend which I had in a file called 
PamBackend.py.
I listed it under AUTHENTICATION_BACKENDS as 'auth_backends.PamBackend'. 
 This
seemed to work ok--authentication was happening as expected, returning a 
valid
user--but the request.user would disappear after a page redirect.

The key is that the backend name is set in the session when authenticate() 
is
called (in django.contrib.auth).  It sets the name like this:

   user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)

So in my case, the name was set to 'auth_backends.PamBackend.PamBackend'.

For a new http request, django checks the session and gets:
1) the name of the auth backend, and
2) the user id.
It then calls get_user (in django/contrib/auth/__init__.py) which does the 
following:
1) gets the backend name from the session,
2) checks for a match in settings, and
3) calls the appropriate get_user method from the appropriate backend.

So at this point, my backend name did not match what was in settings.py and
it returned an anonymous user.

-Scott

On Monday, October 14, 2013 8:52:57 AM UTC-4, HM wrote:
>
> I can't get logging in with alternate auth backends to work with Django 
> 1.4 or newer.
>
> Basically:
>
> 1. authenticate() works. request.user.is_authenticated() is True
> 2. Django's own login() seems to work, as request.user is set, 
> request.session is set etc.
> 3. After the HttpResponseRedirect, request.user is AnonymousUser
>
> This has happened twice now, with different alternate auth backends. I 
> can't get rid of Django 1.3 before this is fixed...
>
> In one project I can call the next view directly (with render) and it Just 
> Works. In the current project, the next view contains a form, and the 
> "login" doesn't survive the POST.
>
> What am I missing? See also the non-solution in 
> http://stackoverflow.com/questions/16119155/django-request-user-not-set-after-redirect
>
>
> HM
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/7e32eb77-6b2f-4d02-9b92-d9b2f667a3a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to