简体   繁体   中英

custom email authentication backend not working in django 2.1.4

I am having trouble in integrating custom authentication backend in django 2.1.4 . Following is my code :
my FMS.authBackend module :

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

class authEmailBackend(ModelBackend):
    def authenticate(self, username=None, password=None, **kwargs):
        print("aaaaaaa")
        UserModel = get_user_model()
        try:
            user = UserModel.objects.get(email=username)
        except UserModel.DoesNotExist:
            return None
        else:
            if user.check_password(password):
                return user
        return None

my settings.py :

AUTHENTICATION_BACKENDS = (
                'FMS.authBackend.authEmailBackend',
                'django.contrib.auth.backends.ModelBackend',

                )

my urls.py :

from django.contrib.auth import views as auth_views
urlpatterns = [ 
        path('login', my_decos.logout_required(auth_views.LoginView.as_view(template_name = 'register/login.html')),name = 'login')
]

The above code is not working in my case. Function authenticate in authEmailBackend is never called as nothing printed in console but I print statement in authenticate function.

although the same code was working for django 2.0.8 , the only difference then was that the urls.py was :

from django.contrib.auth import views as auth_views
urlpatterns = [ 
        path('login', my_decos.logout_required(auth_views.login(template_name = 'register/login.html')),name = 'login')
]

but in the newer django the django.contrib.auth.views.login doesn't support any more and we need to use django.contrib.auth.views.LoginView . I read somewhere that to use custom AUTHENTICATION_BACKEND our url must point to django.contrib.auth.views.login but which is not possible here.

So can you please help me to overcome the problem.

request argument need to pass to the authenticate method

  class authEmailBackend(ModelBackend):
        **def authenticate(self, request, username=None, password=None, **kwargs):**
            print("aaaaaaa")
            UserModel = get_user_model()
            try:
                user = UserModel.objects.get(email=username)
            except UserModel.DoesNotExist:
                return None
            else:
                if user.check_password(password):
                    return user
            return None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM