26 lines
816 B
Python
26 lines
816 B
Python
|
|
from django.contrib.auth import get_user_model
|
||
|
|
from django.contrib.auth.backends import ModelBackend
|
||
|
|
from django.db.models import Q
|
||
|
|
|
||
|
|
User = get_user_model()
|
||
|
|
|
||
|
|
|
||
|
|
class MultiFieldBackend(ModelBackend):
|
||
|
|
"""Authenticate with username, email, or phone_number."""
|
||
|
|
|
||
|
|
def authenticate(self, request, username=None, password=None, **kwargs):
|
||
|
|
if username is None or password is None:
|
||
|
|
return None
|
||
|
|
|
||
|
|
try:
|
||
|
|
user = User.objects.get(
|
||
|
|
Q(username=username) | Q(email=username) | Q(phone_number=username)
|
||
|
|
)
|
||
|
|
except (User.DoesNotExist, User.MultipleObjectsReturned):
|
||
|
|
User().set_password(password)
|
||
|
|
return None
|
||
|
|
|
||
|
|
if user.check_password(password) and self.user_can_authenticate(user):
|
||
|
|
return user
|
||
|
|
return None
|