6

Using django-socialregistration, got following error:

'AnonymousUser' object has no attribute 'backend'

How,

  1. I click on facebook connect url.
  2. That took me Facebook and ask me to login. So I did, asked permission, I granted.
  3. After that it redirect me to my site. And ask to setup. I provide user and email address.
  4. Once I submit, got error like above:

Trace point:

path/to_file/socialregistration/views.py in post
128.      self.login(request, user)

Do anybody know, what's wrong?

Elisa
  • 6,865
  • 11
  • 41
  • 56
  • Your object of `AnonymousUser` class does not have `backend` attribute. This is what is wrong. – Tadeck Nov 30 '11 at 05:02
  • I know that. My question is why django-socialregistration couldn't get valid user, but it get AnonymousUser – Elisa Nov 30 '11 at 05:08
  • Could you paste the complete tracback ? – meson10 Nov 30 '11 at 05:14
  • Thanks meson10, it worked now. My silly mistake that I forget following settings: AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'socialregistration.contrib.facebook.auth.FacebookAuth') – Elisa Nov 30 '11 at 05:25
  • Hi , did you have any progress on this. ? I am facing similar difficulty. I am not using any social autentication though. I am just using django.contrib.auth.login – Vasif Sep 29 '14 at 03:19
  • See my previous comment (just above urs), I got it fixed – Elisa Sep 29 '14 at 04:15

3 Answers3

8

Oh man i used to get this error all the time, basically you are calling

self.login(request, user)

without calling

authenticate(username=user, password=pwd)

first

when you call authenticate, django sets the backend attribute on the user, noting which backend to use, see here for more details https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate

Paulo
  • 6,982
  • 7
  • 42
  • 56
  • 2
    No Paulo, that's not my case. My case is that my silly mistake that I forget following settings: AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'socialregistration.contrib.facebook.auth.FacebookAuth') – Elisa Dec 01 '11 at 05:23
1

I had the same error for a newly registering user.

def attempt_login(self, email, password):
    user = authenticate(username=email, password=password)
    login(self.request, user)

    return user 

I checked into database and the User has been created after registration, but this error was still there.

I figured out - user's login ( email ) was longer than 30 characters, and the form field had no validation. The username would get truncated in the database, and therefore authenticate was called for non-existent login.

254 - character is the advised length of email field.

Solution: emailfield-max_length-r11092.patch

Community
  • 1
  • 1
flyankur
  • 354
  • 3
  • 7
1

I just got this error and found this post.. My solution was in the case was in the registration process. When the user was registering, my api and serializer wasn't hashing the password.. So in the api_view i had to manually hash the password like this..

    from django.contrib.auth.hashers import make_password

    # In the register api..
    @ensure_csrf_cookie
    @api_view(['POST'])
    def register_api(request):

        # Anywhere before the serializer
        request.DATA['password'] = make_password(request.DATA['password'])

        # Then the serializer
        serializer = RegisterSerializer(data=request.DATA)

        # ... etc.. Note that if you want to login after register you will have
        # to store the initial password is some buffer because.. authentication
        # the none hashed version.. then
             authenticate(username=request.DATA['username'], password=someBuffer)

Hope that helps someone..

  • Couple notes for you. If you use DATA instead of headers to send passwords — these passwords are recorded in log-files of the server and the server of the sender. So, use headers instead for password transfer: `new_password = request.headers.get['password']`. Also, it worth use the special function: `user = User.objects.create_user(username=username, email=email, password=new_password)` – Constantine Kurbatov Jun 14 '21 at 22:31