backends.py 770 B

1234567891011121314151617181920212223
  1. from django.contrib.auth.backends import ModelBackend
  2. from django.contrib.auth import get_user_model
  3. class CustomAuthenticationBackend(ModelBackend):
  4. def authenticate(self, request, **credentials):
  5. ret = self.authenticate_by_phone(request, **credentials)
  6. return ret
  7. def authenticate_by_phone(self, request, **credentials):
  8. phone_number = credentials.get("phone_number")
  9. password = credentials.get("password")
  10. User = get_user_model()
  11. if phone_number is None or password is None:
  12. return None
  13. try:
  14. user = User.objects.get(phone_number=phone_number)
  15. if user.check_password(password):
  16. return user
  17. except User.DoesNotExist:
  18. return None