You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

25 lines
755 B

from django.contrib.auth.backends import BaseBackend
from django.db.models import Q
from apps.account.models import User
from utils.exceptions import UserNotFoundException
from rest_framework.exceptions import AuthenticationFailed
class CustomLoginBackend(BaseBackend):
"""
Authenticate with username email and phone_number.
"""
def authenticate(self, request, username=None, password=None):
if user := self.get_user(username):
if user.check_password(password):
return user
return None
def get_user(self, username):
try:
return User.objects.filter(Q(email=username) | Q(phone_number=username)).first()
except Exception.DoesNotExist:
return None