Jump to content

LoginRegister doesn't call loginSuccess()


suntrop
 Share

Recommended Posts

Hi all

I am using the login form

$modules->get('LoginRegister')->execute()

and have in /site/init.php

$session->addHookAfter('loginSuccess', function($event) {
    // Update user's last login time
    $user = $event->arguments(0);
    $user->details->last_login = time();
    $user->of(false);
    $user->save('details');

    if(wire('page')->template == 'admin') return;
    wire('session')->redirect('/dashboard/');
	exit();
});

But somehow, the last_login subfield doesn't update, if I use the Login/Register module login. However, it updates using "PW's default login".

Any idea why? Seems to be the only reliable method to check if the user logged in successfully. The other one, afterLogin, runs for failed logins as well, as I can see.

Link to comment
Share on other sites

I'm new to PW and just learning hooks myself but this is how I got the loginSuccess hook to work in site/_init.php.

Note this only sets a session variable but it is proof that the hook is there and working.

wire()->addHook('LoginRegister::loginSuccess', function($event) { // outside a class
    $session = wire('session');
    $session->set('Login_Register_hook', 'success');
});

output:
  [Login_Register_hook] => success

wire()->addHook('LoginRegister::loginFail', function($event) {
    $session = wire('session');
    $session->set('Login_Register_hook', 'failure');
});

output:

[Login_Register_hook] => failure

  • Like 1
Link to comment
Share on other sites

Well, you are right. Thanks for pointing me the right direction. 

Had the $user->of(false) on the wrong line and just needed to move it before setting the field value. Don't know, why this worked on the default login.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...