# # settings.py
AUTHENTICATION_BACKENDS = (
'utils.auth_backend.MyCustomBackend',
)
# # auth_backend.py
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from django.contrib.auth import get_user_model
from rest_framework.exceptions import AuthenticationFailed
UserModel = get_user_model()
class MyCustomBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = UserModel.objects.get(Q(username=username) | Q(email=username))
if user.check_password(password):
return user
else:
raise AuthenticationFailed({'detail': '账号或密码错误'})
except UserModel.DoesNotExist:
return AuthenticationFailed({'detail': "账号未注册"})
rest-framework多方式登录认证
于 2022-12-23 14:51:23 首次发布