Last active
May 26, 2021 09:43
-
-
Save therealak12/7ecfc68caa642d55fc9b0b88d6848f28 to your computer and use it in GitHub Desktop.
django email auth backend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth import get_user_model | |
from django.contrib.auth.backends import ModelBackend | |
class EmailBackend(ModelBackend): | |
def authenticate(self, request, **kwargs): | |
UserModel = get_user_model() | |
try: | |
email = kwargs.get('email', None) | |
if email is None: | |
email = kwargs.get('username', None) | |
user = UserModel.objects.get(email=email) | |
if user.check_password(kwargs.get('password', None)): | |
return user | |
except UserModel.DoesNotExist: | |
return None | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment