I am working on a Django project in which I have defined a custom user model for which I have required to write the custom authentication method, by following the documentation I have written it like following But I have a problem in calling it in the views.py kindly help me by looking in the following code
I have defined my custom backend as follows
My Custom Authentication Backend
from django.contrib.auth.backends import BaseBackend
from .models import User
from IntellerMatrix.CommonUtilities.constants import Constants
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, email=None, password=None):
user = User.objects.get(email=email)
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
else:
return "User is not activated"
else:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
settings.py
AUTHENTICATION_BACKENDS = ['Modules.users.authentication.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend', ]
Views.py
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = # how to call here that custom authentication backend's authenticate method
if user is None:
return HttpResponse("<p>Not Valid</p>")
else:
return HttpResponse(user)