35

first of i am new to django/python .

i am trying to create a login website that allows the user to register an account and verify via email or directly login via fb or google(Oauth)

i receive the error when i click on the validation url sent to the email.

error:

ValueError at /activate/Mjk/4p1-dcc5f7ed2e7c847fe362/

You have multiple authentication backends configured and therefore must 

provide the `backend` argument or set the `backend` attribute on the user.

Request Method: GET

Request URL:    http://127.0.0.1:8000/activate/Mjk/4p1-dcc5f7ed2e7c847fe362/
Django Version: 1.11.3

Exception Type: ValueError

Exception Value:    

You have multiple authentication backends configured and therefore must provide the `backend` argument or set the `backend` attribute on the user.
Exception Location: /usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py in login, line 149
Python Executable:  /usr/bin/python
Python Version: 2.7.12
Python Path:    
['/home/gaby/django projects/simple-signup-master/profile-model',
 '/usr/local/lib/python2.7/dist-packages/virtualenv-15.1.0-py2.7.egg',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/home/gaby/.local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']
Server time:    Wed, 30 Aug 2017 12:34:31 +0000

mysite/settings

AUTHENTICATION_BACKENDS = (

    
     'social_core.backends.facebook.FacebookOAuth2',
     'social_core.backends.google.GoogleOAuth2',

     'django.contrib.auth.backends.ModelBackend',
)

this is the function being called when i receive the error

    def activate(request, uidb64, token):
    try:
    uid = force_text(urlsafe_base64_decode(uidb64))
    user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
    user = None

    if user is not None and account_activation_token.check_token(user, 
    token):
    user.is_active = True
    user.profile.email_confirmed = True
    user.save()
    login(request, user)
    return redirect('home')
else:
    return render(request, 'account_activation_invalid.html')

i started receiving the error after adding the oath login via fb,google. if i remove'social_core.backends.facebook.FacebookOAuth2','social_core.backends.google.GoogleOAuth2' from AUTHENTICATION_BACKENDS the email confirmation will work again.

i searched online and also here the only solution that i found was below but it didn't solve my problem.

here

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
gaby awad
  • 1,068
  • 1
  • 9
  • 17

6 Answers6

50

I found the solution. call login() with backend argument like login(request, user, backend='django.contrib.auth.backends.ModelBackend')

here is full code:

def activate(request, uidb64, token, backend='django.contrib.auth.backends.ModelBackend'):
    try:
        uid = force_text(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.profile.email_confirmed = True
        user.save()
        login(request, user, backend='django.contrib.auth.backends.ModelBackend')
        return redirect('home')
    else:
        return render(request, 'account_activation_invalid.html')
Smit Patel
  • 63
  • 5
gaby awad
  • 1,068
  • 1
  • 9
  • 17
12

So in my case I simply hadn't registered the user. Also did login(request, user, backend='django.contrib.auth.backends.ModelBackend')

To register user:

# register.py
from django.contrib.auth.models import User
cfg_user = 'user'
user = User.objects.create_user(username=cfg_user,
                                 email='email@domain.com',
                                 password='password')
print('registered user '+cfg_user)

# register.sh
python manage.py shell < register.py
citynorman
  • 4,918
  • 3
  • 38
  • 39
2

If you are using the build in django class PasswordResetConfirmView, then you can fix it by adding:

post_reset_login_backend = "django.contrib.auth.backends.RemoteUserBackend"

Something like so:

from django.contrib.auth import views

class PasswordResetConfirmView(views.PasswordResetConfirmView):
    template_name = "reset_confirm.html"
    success_url = "/"
    post_reset_login = True
    post_reset_login_backend = "django.contrib.auth.backends.RemoteUserBackend"
Christoffer
  • 7,436
  • 4
  • 40
  • 42
1

If you set multiple authentication backends in settings.py as shown below:

# "core/settings.py"

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "graphql_jwt.backends.JSONWebTokenBackend",
]

You need to set backend='django.contrib.auth.backends.ModelBackend' to login() in views.py as shown below then the error will be solved:

# "account/views.py"

from django.shortcuts import render, redirect
from .forms import NewUserForm
from django.contrib.auth import login
from django.contrib import messages

def register_request(request):
    if request.method == "POST":
        form = NewUserForm(request.POST)
        if form.is_valid():
            user = form.save()   # Here
            login(request, user, backend='django.contrib.auth.backends.ModelBackend')
            messages.success(request, "Registration successful.")
            return redirect("account:homepage")
        messages.error(request, "Unsuccessful registration. Invalid information.")
    form = NewUserForm()
    return render(request, "account/register.html", {"register_form":form})

In addition, when I set backend='graphql_jwt.backends.JSONWebTokenBackend' to login() in views.py as shown below then the error was solved as well in my case so if you set one of multiple authentication backends, the error may be solved as well:

# "account/views.py"

from django.shortcuts import render, redirect
from .forms import NewUserForm
from django.contrib.auth import login
from django.contrib import messages

def register_request(request):
    if request.method == "POST":
        form = NewUserForm(request.POST)
        if form.is_valid():
            user = form.save()   # Here
            login(request, user, backend='graphql_jwt.backends.JSONWebTokenBackend')
            messages.success(request, "Registration successful." )
            return redirect("account:homepage")
        messages.error(request, "Unsuccessful registration. Invalid information.")
    form = NewUserForm()
    return render(request, "account/register.html", {"register_form":form})

And, if you set only one "django.contrib.auth.backends.ModelBackend" to AUTHENTICATION_BACKENDS as shown below:

# "core/settings.py"

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
]

Or, if you don't explicitly set AUTHENTICATION_BACKENDS itself in settings.py as shown below:

# "core/settings.py"

# AUTHENTICATION_BACKENDS = [
#     "django.contrib.auth.backends.ModelBackend",
# ]

You don't need to set backend='django.contrib.auth.backends.ModelBackend' to login() in views.py because "django.contrib.auth.backends.ModelBackend" is set automatically.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
0

In my case just simple assigning some of backend class from Django configuration to user work properly:

setattr(user, 'backend', 'django.contrib.auth.backends.RemoteUserBackend')

and then you can authenticate your user

Rafał
  • 572
  • 4
  • 21
0

As suggested here you need to set the backend of the user record. Before login(request, user) add the following line:

user.backend = 'django.contrib.auth.backends.ModelBackend'
...
login(request, user)