django 2.1.2后端身份验证失败

时间:2018-10-27 10:17:32

标签: django django-views

我正在尝试使用后端身份验证添加项来使用户通过电子邮件进行身份验证,而用户无法通过电子邮件进行身份验证并返回“无”

backends.py

from django.contrib.auth.models import User


class EmailAuth():
    """Authenticate a user by an exact match on the email and password"""

    def authenticate(self, username=None, password=None):
        """
        Get an instance of `User` based off the email and verify the
        password
        """

        try:
            user = User.objects.get(email=username)

            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None

    def get_user(self, user_id):
        """
        Used by the Django authentiation system to retrieve a user instance
        """

        try:
            user = User.objects.get(pk=user_id)
            if user.is_valid():
                return user
            return None
        except User.DoesNotExist:
            return None

accounts / settings.py

AUTHENTICATION_BACKENDS = [
'accounts.backends.EmailAuth',
'django.contrib.auth.backends.ModelBackend'
]

accounts / views.py摘录,用户为“无”,但用户名和密码已传递给身份验证功能

def login_by_email(request):
    """Return a login page"""
    if request.user.is_authenticated:
        return redirect(reverse('index'))
    if request.method == "POST":
        login_form = UserLoginForm(request.POST)

        if login_form.is_valid():
            user = auth.authenticate(username=request.POST['username'],
                                    password=request.POST['password'])

            # import pdb; pdb.set_trace()

            if user:
                login(user=user, request=request)
                # login(request, form.get_user())
                messages.success(request, "You have successfully logged in!")
                return redirect(reverse('index'))
            else:
                login_form.add_error(None, "Your username or password is incorrect")
    else:
        login_form = UserLoginForm()
    return render(request, 'login.html', {'login_form': login_form})

account / urls.py

from django.conf.urls import url, include
from accounts.views import logout, login_by_email, registration, user_profile
from accounts import urls_reset

urlpatterns = [
    url(r'^logout/', logout, name="logout"),
    url(r'^login/', login_by_email, name="login"),
    url(r'^register/', registration, name="registration"),
    url(r'^profile/', user_profile, name="profile"),
    url(r'', include(urls_reset))
]

根项目urls.py

from django.conf.urls import url, include
from django.contrib import admin
from django.urls import path
from accounts.views import index
from accounts import urls as accounts_urls

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', index, name="index"),
    url(r'^accounts/', include(accounts_urls))
]
在Ciaran Kehoe的帮助下,

工作解决方案

backends.py

from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User

class EmailAuth():
    """
    Authenticate a user by an exact match on the email and password
    """

    def authenticate(self, request, username=None, password=None):

        try:
            user = User.objects.get(email=username)

            if user.check_password(password):
                return user
            return None
        except User.DoesNotExist:
            return None


    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None

accounts / views.py

def login_by_email(request):
    """Return a login page"""
    if request.user.is_authenticated:
        return redirect(reverse('index'))
    if request.method == "POST":
        login_form = UserLoginForm(request.POST)
    if login_form.is_valid():
        user = auth.authenticate(request, username=request.POST['username'],
                                password=request.POST['password'])

        if user:
            login(request, user, backend='accounts.backends.EmailAuth')
            messages.success(request, "You have successfully logged in!")
            return redirect(reverse('index'))
        else:
            login_form.add_error(None, "Your username or password is incorrect")
else:
    login_form = UserLoginForm()
return render(request, 'login.html', {'login_form': login_form})

1 个答案:

答案 0 :(得分:1)

您只需要将requestauthenticate一起传递。

def authenticate(self, request, username=None, password=None):

    try:
        user = User.objects.get(email=username)

        if user.check_password(password):
            return user
        return None
    except User.DoesNotExist:
        return None