Skip to content

Instantly share code, notes, and snippets.

@osw4l
Created August 6, 2020 02:54
Show Gist options
  • Save osw4l/7581c4347b7aa3d58523f510d8eba269 to your computer and use it in GitHub Desktop.
Save osw4l/7581c4347b7aa3d58523f510d8eba269 to your computer and use it in GitHub Desktop.
custom auth backend django
class CustomAuthenticationBackend:
def authenticate(self, request, email_or_phone=None, password=None):
try:
user = User.objects.get(
Q(email=email_or_phone) | Q(phone=email_or_phone)
)
pwd_valid = user.check_password(password)
if pwd_valid:
return user
return None
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