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.
29 lines
987 B
29 lines
987 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, email=None, **kwargs):
|
|
identifier = username or email or kwargs.get('username') or kwargs.get('email')
|
|
if user := self.get_user(identifier):
|
|
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
|