Django Signals on Login

What is django signal in login function? The user has already been added to the query session table. So what is the last line call for signal in the Django auth.login function?

 @sensitive_post_parameters() 
 @csrf_protect
 @never_cache
 def login(request, user):
  """
  Persist a user id and a backend in the request. This way a user doesn't
  have to reauthenticate on every request. Note that data set during
  the anonymous session is retained when the user logs in.
  """
  session_auth_hash = ''
  if user is None:
     user = request.user
  if hasattr(user, 'get_session_auth_hash'):
     session_auth_hash = user.get_session_auth_hash()

  if SESSION_KEY in request.session:
    if _get_user_session_key(request) != user.pk or (
            session_auth_hash and
            request.session.get(HASH_SESSION_KEY) != session_auth_hash):
        # To avoid reusing another user session, create a new, empty
        # session if the existing session corresponds to a different
        # authenticated user.
        request.session.flush()
  else:
    request.session.cycle_key()
  request.session[SESSION_KEY] = user._meta.pk.value_to_string(user)
  request.session[BACKEND_SESSION_KEY] = user.backend
  request.session[HASH_SESSION_KEY] = session_auth_hash
  if hasattr(request, 'user'):
    request.user = user
  rotate_token(request)
  user_logged_in.send(sender=user.__class__, request=request, user=user)

      

+3


source to share


2 answers


The signal user_logged_in

sent at the end has login()

been defined so that it can be used to notify when a user is logged. It also updates the attribute last_login

for the current user (like @ozgur mentioned above).

From Inputs and Outputs Documentation :

The structure uses two signal auth ( user_logged_in

and user_logged_out

) that can be used to notify a user logs in or out.

The arguments sent with this signal are as follows:

  • sender

    : the class of the user who just logged in.
  • request

    : the current HttpRequest instance.
  • user

    : the user instance that just logged in.

Using this helps in providing untethered application notifications when the user logs on .

Suppose you want to be notified every time a user logs in and takes some action, then you can use this signal to make your recipient receive a notification when they log in. To receive a signal, you need to register a receiver function that is called when a signal is sent using a method Signal.connect()

.



For example:

perform_some_action_on_login

is a receiver function that will perform some additional actions whenever a user logs in.

from django.contrib.auth.signals import user_logged_in

def perform_some_action_on_login(sender, user, **kwargs):
    """
    A signal receiver which performs some actions for
    the user logging in.
    """
    ...
    # your code here

      

After defining the registration function, connect the receiver function with the signal user_logged_in

user_logged_in.connect(perform_some_action_on_login)

      

Now when the user logs in, a signal is sent user_logged_in

, which is received by the receiver function perform_some_actions_on_login

. He then performs these additional steps.

+8


source


try putting the following code in your application's __init__.py



from django.contrib.auth.signals import user_logged_in
from django.dispatch import receiver


@receiver(user_logged_in)
def on_login(sender, user, request, **kwargs):
    print('User just logged in....')

      

0


source







All Articles