0

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?

H.fate
  • 644
  • 2
  • 7
  • 18

1 Answers1

1

There is no reason to add the default backend here, as it does the same as the username part of your custom one.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895