django中的自定义身份验证不起作用

时间:2019-02-12 13:39:53

标签: django python-3.x django-authentication

我是django的新手,我想用emailusernamepassword上对用户进行身份验证,因此我写了一个自定义身份验证,如文档所示,但似乎不是打电话,我不知道该怎么办?

settings.py

AUTHENTICATION_BACKENDS = ('accounts.backend.AuthBackend',)

views.py

def login(request):
    if request.method == 'POST':
        username_or_email = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username_or_email, password=password)
        print(user)
        if user is not None:
            return reverse('task:home')
        else:
            messages.error(request, "Username or password is invalid")
            return render(request, 'accounts/login.html')
    else:
         return render(request, 'accounts/login.html')

backend.py

from django.contrib.auth.models import User
from django.db.models import Q


class AuthBackend(object):
    supports_object_permissions = True
    supports_anonymous_user = False
    supports_inactive_user = False

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

    def authenticate(self, username, password):
        print('inside custom auth')
        try:
            user = User.objects.get(
                Q(username=username) | Q(email=username) )
            print(user)
        except User.DoesNotExist:
            return None
        print(user)
        if user.check_password(password):
             return user
        else:
            return None

我在课堂上写下了这些print语句,以检查它们是否正在控制台中被调用和编写。但是,它们不会被打印,print中的views.py语句会打印None

2 个答案:

答案 0 :(得分:0)

您需要extend中的ModelBackend django.contrib.auth.backends

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

User = get_user_model()

class AuthBackend(ModelBackend):
    supports_object_permissions = True
    supports_anonymous_user = False
    supports_inactive_user = False

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

    def authenticate(self, request, username=None, password=None):
        print('inside custom auth')
        try:
            user = User.objects.get(
                Q(username=username) | Q(email=username) )
            print(user)
        except User.DoesNotExist:
            return None
        print(user)
        if user.check_password(password):
             return user
        else:
            return None

settings.py中,请不要忘记添加自定义后端身份验证

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

另一个可能的解决方案

从您的代码中,我看到的是您希望将email视为User模型的user_name。您可以轻松修改Django's AbstructUser模型,如下所示

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
      # your necessary additional fields 
       USERNAME_FIELD = 'email'  # add this line 

现在email字段将被视为user_name字段。无需添加自定义authentication-backend

答案 1 :(得分:0)

import sys
from django.contrib.auth import get_user_model

from accounts.models import Token

User = get_user_model()


class PasswordlessAuthenticationBackend:

    def authenticate(self, uid=None):
        print('uuuuuuuuu')
        print('uid', uid, file=sys.stderr)
        if not Token.objects.filter(uid=uid).exists():
            print('no token found', file=sys.stderr)
            return None
        token = Token.objects.get(uid=uid)
        print('got token', file=sys.stderr)
        try:
            user = User.objects.get(email=token.email)
            print('got user', file=sys.stderr)
            return user
        except User.DoesNotExist:
            print('new user', file=sys.stderr)
            return User.objects.create(email=token.email)

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