0

I implemented Django signals to handle some actions after login, but I'd also like to be able to cancel the login actions:

I have this:

@receiver(user_logged_in)
def my_signal(sender, **kwargs):
    request, user = kwargs['request'], kwargs['user']
    # some actions

I want something like this:

@receiver(user_logged_in)
def my_signal(sender, **kwargs):
    request, user = kwargs['request'], kwargs['user']
    if "any validation" == True:
        # some actions
    else:
        # cancel login

Is that possible? If so, how can I do it? Well, you can think that I could validate before call auth.login(request, user) but let me know if it is possible. Thanks in advance.

Nagra
  • 466
  • 3
  • 8
Andres
  • 4,323
  • 7
  • 39
  • 53

1 Answers1

0

You can technically use logout(request) to do what you want: https://docs.djangoproject.com/en/1.6/topics/auth/default/#django.contrib.auth.logout

Since you seem to have access to the request object in my_signal, you should be able to do that.

However, this is probably not the best way to accomplish what you want. You shouldn't be authenticating users in the first place rather than authenticating them, logging them in, and then booting them off.

Nagra
  • 466
  • 3
  • 8