from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend class EmailAuthBackend(ModelBackend): """ Email Authentication Backend Allows a user to sign in using an email/password pair rather than a username/password pair. """ def authenticate(self, email=None, password=None): UserModel = get_user_model() try: user = UserModel.on_organization.get(email=email) if user.check_password(password): return user except UserModel.DoesNotExist: # Run the default password hasher once to reduce the timing # difference between an existing and a non-existing user (#20760). UserModel().set_password(password)