Wednesday, November 6, 2024

Re: django.contrib.auth.update_session_auth_hash not working after change password

@cseb Where did you get that Django Code?

On Wed, 6 Nov 2024 at 15:07, Ruby <dev.rubyshell@gmail.com> wrote:
Your code needs to be refactored, here is the real deal, your ChangePassowrdFrom is missing `request`, it should be as it is below
form = grandmas4hire.forms.ChangePasswordForm(request, request.POST)


On Wed, Nov 6, 2024 at 8:20 PM cseb...@gmail.com <cseberino@gmail.com> wrote:
Ruby

Thank you very much.  Here is my code...


INV        = grandmas4hire.models.Invitation  

...

def add_url_param(url, param, arg):                                             
        prefix = "&" if "?" in url else "/?"                                    
                                                                                
        return url + prefix + f'{param}={str(arg).replace(" ", "+")}'           
 
---

@django.contrib.auth.decorators.login_required                                  
def change_password(request):                                                  
        user = request.user                                                    
        msg  = request.GET.get("msg")                                          
        if request.method == "POST":                                            
                form = grandmas4hire.forms.ChangePasswordForm(request.POST)    
                if form.is_valid():                                            
                        new_password = form.cleaned_data["new_password"]        
                        inv          = INV.objects.get(user = user)            
                        inv.user.set_password(new_password)                    
                        inv.user.save()                                        
                        django.contrib.auth.update_session_auth_hash(request,  
                                                                     user)      
                        url          = add_url_param("/change_password",        
                                                     "msg",                    
                                                     "Password+changed.")      
                        reply        = django.shortcuts.redirect(url)          
                else:                                                          
                        reply = django.shortcuts.render(request,                
                                                        "change_password.html",
                                                        {"form" : form})        
        else:                                                                  
                form  = grandmas4hire.forms.ChangePasswordForm()                
                reply = django.shortcuts.render(request,                        
                                                "change_password.html",        
                                                {"form" : form,                
                                                 "msg"  : msg})                
                                                                               
        return reply                          

On Tuesday, November 5, 2024 at 5:41:09 PM UTC-6 Ruby wrote:
How was it implemented?
Show a snippet from your code
See how it was used in my code

form = ChangePasswordForm(request, request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
messages.success(
request, "Your password has been successfully updated")
return redirect(request.META.get('HTTP_REFERER'))

On Tue, Nov 5, 2024 at 10:30 PM cseb...@gmail.com <cseb...@gmail.com> wrote:
When I change a password, users are logged out.
Django recommends keeping users logged in
by calling django.contrib.auth.update_session_auth_hash(request, user).

This is not working in a Django website of mine.
They must log in again!?

There are no error messages.  Is there any way I can provide
more details?

Chris

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/04908d1c-a1e4-41ea-afd8-e227f78af8bcn%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/0c0c2c50-5064-4e9d-a579-f0ddb323c11an%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/CAPUD46tCgtE4bK6yrrZ18FuxE4oTsvkda%2BLG2S8Fz14pxrkb%3DA%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/django-users/CAGiSraBJ4SuFGgJKiMWvuspOM0Sp3%3DKgKVb%2BAfoHQDkG6%3D2HsQ%40mail.gmail.com.

No comments:

Post a Comment