I am trying custom authentication with django, I wrote a class and filled it with the methods authenticate and get_user, I also added this authentication to the AUTHENTICATION_BACKENDS in settings.py file.
I have called authenticate method and followed it up with login in my view.
Everything seems to work fine,
- is_authenticated returns true for the user after login,
- user.backends set to my custom backend.
- sessionid cookie is getting set in my browser
But the subsequent requests have request.user as anonymous, unable to figure out the reason, require your help. Sharing the code below, I am just trying it to learn custom authentication.
views.py
def home(request):
if not request.user.is_authenticated():
user=authenticate(username=None,passwd=None,request=request)
if not user:
return HttpResponse("Login Failed")
else:
login(request,user)
return HttpResponse("Logged in Successfully")
cusauth.py
class CustomloginBackend:
def authenticate(self,username=None,passwd=None,request=None):
return self.get_user("praveen.madhavan")
def get_user(self,username):
try:
return User.objects.get(username=username)
except Exception as e:
return False
What could be the problem ?
Thanks
Praveen.M