Skip to content

Instantly share code, notes, and snippets.

@therealak12
Last active May 26, 2021 09:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealak12/7ecfc68caa642d55fc9b0b88d6848f28 to your computer and use it in GitHub Desktop.
Save therealak12/7ecfc68caa642d55fc9b0b88d6848f28 to your computer and use it in GitHub Desktop.
django email auth backend
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