1234567891011121314151617181920212223 |
- from django.contrib.auth.backends import ModelBackend
- from django.contrib.auth import get_user_model
- class CustomAuthenticationBackend(ModelBackend):
- def authenticate(self, request, **credentials):
- ret = self.authenticate_by_phone(request, **credentials)
- return ret
- def authenticate_by_phone(self, request, **credentials):
- phone_number = credentials.get("phone_number")
- password = credentials.get("password")
- User = get_user_model()
- if phone_number is None or password is None:
- return None
- try:
- user = User.objects.get(phone_number=phone_number)
- if user.check_password(password):
- return user
- except User.DoesNotExist:
- return None
|