python - django user logged out after password change -
i having issue django users changing passwords - have built few production sites in django, none in year (or in 1.8), don't recall having issue before.
summary
when user changes password, user logged out, password changed.
details
i have view allows user change password, using standard django forms , auth framework, , to stress: changing password works, logs user out have login again.
i don't mind terribly, prefer user redirected dashboard message update, if need reauth user in code, will, seems kind of clunky.
here view function:
@login_required def user_change_password(request): """allows user change password""" if request.method == "post": form = subscriberpasswordform(request.post) if form.is_valid(): try: request.user.set_password(form.cleaned_data['password']) request.user.save() except exception, err: print "error changing password: {}".format(err) messages.add_message(request, messages.error, 'the password not changed, please try again ' 'later. admins have been notified of error.') else: #this outputs true print request.user.is_authenticated() messages.add_message(request, messages.info, 'your password has been changed successfully') return httpresponseredirect("/accounts/dashboard/") else: form = subscriberpasswordform() return render(request, "accounts/change-password.html", {"form": form})
so password changed, user gets redirected dashboard page, @login_required decorator redirects them login screen.
the password form here, though pretty straightforward.
class subscriberpasswordform(forms.form): password = forms.charfield(widget=forms.passwordinput) cpassword = forms.charfield(widget=forms.passwordinput) def clean_cpassword(self): password1 = self.cleaned_data.get("password") password2 = self.cleaned_data.get("cpassword") if password1 , password2 , password1 != password2: raise forms.validationerror( self.error_messages['password_mismatch'], code='password_mismatch', )
my understanding being logged out after password change new in django 1.7. need re-auth user in code said.
see release notes: https://docs.djangoproject.com/en/1.8/releases/1.7/#django-contrib-auth
here specific note: "the abstractbaseuser.get_session_auth_hash() method added , if auth_user_model inherits abstractbaseuser, changing user’s password invalidates old sessions if sessionauthenticationmiddleware enabled. see session invalidation on password change more details including upgrade considerations when enabling new middleware."
see documentation: https://docs.djangoproject.com/en/1.7/topics/auth/default/#session-invalidation-on-password-change
Comments
Post a Comment