I am trying to log in to a database that is not the default database, and for that I've wrote a custom authentication code but whenever I try to login the method returns an AnonymousUser. I've no idea why is it doing so because user authentication is done properly using the authenticate method.
Any help would be really appreciated.
MY FILES
views.py
def login_authentication(request):
if request.method == "POST":
form = New_Login_Form(request.POST)
# print(request.POST)
if form.is_valid():
email = request.POST['email']
password = request.POST['password']
user_operating_company = request.POST['user_operating_company']
user = authenticate(request, email=email,
password=password, db=user_operating_company)
if user:
login(request, user, user_operating_company)
return redirect('test')
else:
form = New_Login_Form()
return render(request, 'index.html', {'form': form})
backends.py
from django.contrib.auth.backends import ModelBackend
from .models import Account
class CustomAuthenticate(ModelBackend):
def authenticate(self, request, email=None, password=None, db=None):
try:
user = Account.objects.all().using(db).get(email=email)
if user.check_password(password):
return user
except:
return None
def get_user(self, request, email, db):
try:
return Account.objects.using(db).get(pk=email)
except:
return None
and in the
settings.py
AUTHENTICATION_BACKENDS = ('accounts.backends.CustomAuthenticate', 'django.contrib.auth.backends.ModelBackend')
EDIT:
I made the changes as per @schillingt 's answer the updated backend is:
from django.contrib.auth.backends import ModelBackend
from .models import Account
class CustomAuthenticate(ModelBackend):
def authenticate(self, request, email=None, password=None, db=None):
self.db = db
try:
user = Account.objects.using(db).get(email=email)
if user.check_password(password):
return user
except Account.DoesNotExist:
return None
def get_user(self, email):
try:
user = Account.objects.using(self.db).get(pk=email)
except Account.DoesNotExist:
return None
return user if self.user_can_authenticate(user) else None
But now it gives me an error which says
'CustomAuthenticate' object has no attribute 'db'