Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # In any authentication related app create new file email_authentication_backends.py
- from django.contrib.auth.models import User
- from django.contrib.auth.backends import ModelBackend
- class EmailAuthBackend(ModelBackend):
- """
- Extends default django ModelBackend for both Email Authentication and Username
- Authentication against settings.AUTH_USER_MODEL
- i.e: user can login with either email or username.
- """
- def authenticate(self, username=None, password=None, **kwargs):
- if username is None:
- username = kwargs.get('username')
- try:
- user = User.objects.get(email=username)
- except User.DoesNotExist:
- # Run the default password hasher once to reduce the timing
- # difference between an existing and a non-existing user
- User().set_password(password)
- else:
- if user.check_password(password) and self.user_can_authenticate(user):
- return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement