backends.py 644 B

12345678910111213141516
  1. from django.contrib.auth import get_user_model
  2. from django.contrib.auth.backends import ModelBackend
  3. class EmailBackend(ModelBackend):
  4. def authenticate(self, request, username=None, password=None, **kwargs):
  5. if not username:
  6. return None
  7. UserModel = get_user_model()
  8. try:
  9. user = UserModel._default_manager.get(email__iexact=username)
  10. except UserModel.DoesNotExist:
  11. UserModel().set_password(password) # https://code.djangoproject.com/ticket/20760
  12. else:
  13. if user.check_password(password) and self.user_can_authenticate(user):
  14. return user