I read it(Log in user using either email address or username in Django) and use this backend
custom backend:
from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.auth.backends import ModelBackend
class EmailOrUsernameModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
if '@' in username:
kwargs = {'email': username}
else:
kwargs = {'username': username}
try:
user = User.objects.get(**kwargs)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
and settings.py:
AUTHENTICATION_BACKENDS=[
'logintest.custombackend.EmailOrUsernameModelBackend',
]
Although it works well but I wonder whether I should use backend like this:
AUTHENTICATION_BACKENDS=[
'logintest.custombackend.EmailOrUsernameModelBackend',
'django.contrib.auth.backends.ModelBackend'
]
Do I use it both backends? or It's ok for just one custom backend?