I authenticate the user in this view:
def note_edit_auth(request):
if request.method == 'POST':
form = EditAuthForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = auth.authenticate(username = email, password = password)
if user and user.is_authenticated():
request.session['email'] = email
request.session['password'] = password
request.session['user'] = user
return HttpResponseRedirect('/note_edit')
else:
return HttpResponse('User or password is either incorrect or does not exist.')
And it works. user is indeed authenticated. So then I try to access request.user from another view where the above view redirects to like this:
def note_edit(request):
if request.method == 'POST' and request.user.is_authenticated():
form_edit = EditForm(request.POST)
user = request.session['email']
auth.logout(request)
return HttpResponse('Done!')
elif request.method == 'GET' and request.user.is_authenticated():
form_edit = EditForm(irrelevantfoofoo)
else:
return HttpResponse('User not authenticated.')
return render_to_response('editnote.htm', {'form': form_edit}, context_instance = RequestContext(request))
And suddenly I get the response that user is not authenticated. When I tried printing request.user to console it is showing as AnonymousUser. And I kid you not this was working fine last night. Today I wake up, make some changes and it starts misbehaving. I revert all changes and it still misbehaves.
Why is this happening and how do I fix it?
auth.authenticate()just checks if the provided user credentials are valid.To actually log the user in you have to use
login().