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.
28 lines
875 B
28 lines
875 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:
|
|
if isinstance(username, int):
|
|
return User.objects.filter(id=int(username)).first()
|
|
return User.objects.filter(Q(email=username) | Q(phone_number=str(username))).first()
|
|
|
|
except User.DoesNotExist:
|
|
return None
|