0

What I am trying to do ?

I am trying to redirect a new user to login dialog after user sets a password for the first time. (I am doing this because the movement user sets a password Django implicitly logout the user)

What is the problem ?

For some reason the password_set signal doesn't seem to work. i.e the sender function loginAfterPassChange doesn't get executed.

My Code:

How do I set password

views.py:

@receiver(user_logged_in)
def getFirstTimePass(user, request, **kwargs): #this works
    if user.profile.provider != '' and user.profile.firstTimeLogin:
        user.profile.firstTimeLogin = False
        user.profile.save()
        raise ImmediateHttpResponse(render(request, 'index.html', {'type': user.profile.provider, 'email': user.email}))

@receiver(password_set)
def loginAfterPassChange(request, user, **kwargs): #this doesn't work
    data = {'msg': 'Please login with your new password.'}
    return HttpResponse(data)

def setPassword(request): #this works
    data = {'errMsg': 'Something went wrong.'}
    if not request.user.has_usable_password():
            password = request.POST.get('password')         
            request.user.set_password(password)
            request.user.save()
            data['errMsg'] = ''
    return JsonResponse(data)   

urls.py:

from django.conf.urls import url
from .import views

urlpatterns = [
url(r'^updatePro', views.setPassword, name='updatePro') 
]

models.py:

class Profile(models.Model):
   user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE)  
   provider = models.CharField(max_length=256, default='')
   firstTimeLogin = models.BooleanField(default=True)

if user.profile.provider != '' Check to see if user is logged in with social account or local account.

Ahtisham
  • 9,170
  • 4
  • 43
  • 57

1 Answers1

1

You can't use a signal for this. Django doesn't do anything with the return value from a signal, so it won't redirect. This simply isn't what signals are for. You should have all this logic in the view.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Its not only `return` that doesn't work the whole function doesn't work. If I write `print "Daniel Roseman"` befor return I don't see it in the console. – Ahtisham Jan 17 '18 at 11:56
  • What is `password_set`? Where is it defined? – Daniel Roseman Jan 17 '18 at 11:58
  • `password_set` is a django allauth signal that is sent when a password has been successfully set for the first time. http://django-allauth.readthedocs.io/en/latest/signals.html – Ahtisham Jan 17 '18 at 12:01
  • When you set a password using the all-auth flow, which you're not doing. – Daniel Roseman Jan 17 '18 at 12:04
  • I don't get you. Did you mean how am I setting the password ? – Ahtisham Jan 17 '18 at 12:05
  • Yes. Signals are only sent if something triggers them, and the thing that triggers allauth's password_set signal is [allauth's own PasswordSet view](https://github.com/pennersr/django-allauth/blob/c4864dc217ef4a106e58d5ddabe88fba1dae1a4c/allauth/account/views.py#L584). Since you're not using that, you can't expect the signal to be sent. Regardless, once again *this is not what signals are for* and you should just put the logic in your view. – Daniel Roseman Jan 17 '18 at 12:10