Skip to content

Instantly share code, notes, and snippets.

@legitYosal
Created May 12, 2020 15:44
Show Gist options
  • Save legitYosal/a055a5b67e4c62273bb27339ca5205fd to your computer and use it in GitHub Desktop.
Save legitYosal/a055a5b67e4c62273bb27339ca5205fd to your computer and use it in GitHub Desktop.
Django custom authenticate backend for login user by username or email
class EmailandUsernameAuthBackend(object):
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(Q(username=username) | Q(email=username))
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment