Guest User

Untitled

a guest
Aug 15th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. from django.contrib.auth.backends import ModelBackend
  2. from django.contrib.auth.models import User
  3.  
  4. class EmailorUsernameBackend(ModelBackend):
  5. """
  6. Authenticate given a users email or user name.
  7. Does a double look up on email and user name but would rather
  8. call super for ModelBackend to keep up with any changes.
  9.  
  10. put the following in your settings.py
  11. AUTHENTICATION_BACKENDS = ('yourproject.app.backend.EmailorUsernameBackend',)
  12.  
  13. """
  14.  
  15. def authenticate(self, username=None, password=None):
  16. try:
  17. #probably should be more stricy about email format
  18. if '@' in username:
  19. user = User.objects.get(email=username)
  20. username = user.username
  21.  
  22. except User.DoesNotExist:
  23. return None
  24.  
  25. return super(EmailorUsernameBackend, self).authenticate(username=username, password=password)
Add Comment
Please, Sign In to add comment