Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # In any authentication related app create new file email_authentication_backends.py
  2. from django.contrib.auth.models import User
  3. from django.contrib.auth.backends import ModelBackend
  4.  
  5. class EmailAuthBackend(ModelBackend):
  6. """
  7. Extends default django ModelBackend for both Email Authentication and Username
  8. Authentication against settings.AUTH_USER_MODEL
  9.  
  10. i.e: user can login with either email or username.
  11.  
  12. """
  13. def authenticate(self, username=None, password=None, **kwargs):
  14. if username is None:
  15. username = kwargs.get('username')
  16. try:
  17. user = User.objects.get(email=username)
  18. except User.DoesNotExist:
  19. # Run the default password hasher once to reduce the timing
  20. # difference between an existing and a non-existing user
  21. User().set_password(password)
  22.  
  23. else:
  24. if user.check_password(password) and self.user_can_authenticate(user):
  25. return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement