Yii Framework Forum: Using Afterlogin To Save User Login Time - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Using Afterlogin To Save User Login Time

#1 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 12-December 12

Posted 23 November 2013 - 01:06 PM

Hi,

I'm trying to extend the built-in afterLogin method from \Web\User.php to save the user's last login time.

I've put this code in my User model, but it is not called:
protected function afterLogin($identity, $cookieBased) {
	if(parent::afterLogin($identity, $cookieBased)) {
		$this->scenario = 'login';
		$this->last_login_timestamp = time();
		$this->save();
		return true;
	}
	return false;
}


My User class extends \yii\db\ActiveRecord and implements IdentityInterface.

Any clue what I am doing wrong?
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,649
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 November 2013 - 01:20 PM

I suppose parent::afterLogin does not return true, so your code block is not executed.
God is real unless declared as integer
0

#3 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 12-December 12

Posted 23 November 2013 - 04:30 PM

View PostORey, on 23 November 2013 - 01:20 PM, said:

I suppose parent::afterLogin does not return true, so your code block is not executed.

I also considered that, so I moved my code above the if block, but this also did not work...
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,649
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 November 2013 - 05:12 PM

My bad. It is not the identity event, it's user component's.

You can subscribe to it somewhere in app (for example, in module init() or wherever you like), like this:

Yii::$app->user->on('afterLogin', function($event) {
    echo 'testing event'; 
    exit;
});

God is real unless declared as integer
0

#5 User is offline   amnah 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 23-February 13

Posted 23 November 2013 - 08:50 PM

I imagine that it's failing validation and thus not saving. Have you checked if it actually reaches that code block?
0

#6 User is offline   DocSolver 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 12-December 12

Posted 24 November 2013 - 07:55 AM

I'm not sure. By now I have reverted to another approach: I moved this code into the LoginForm model as part of the login method.
0

#7 User is offline   Tropi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 73
  • Joined: 30-November 11

Posted 24 November 2013 - 08:28 AM

Why? Better start debugging, maybe it's a bug in Yii. There's only a few methods involved anyway, just follow the flow in LoginForm::login(), User::login(), User::beforeLogin() and User::afterLogin(). And maybe Component::trigger() itself.
0

#8 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,649
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 24 November 2013 - 08:32 AM

View PostTropi, on 24 November 2013 - 08:28 AM, said:

Why? Better start debugging, maybe it's a bug in Yii. There's only a few methods involved anyway, just follow the flow in LoginForm::login(), User::login(), User::beforeLogin() and User::afterLogin(). And maybe Component::trigger() itself.


It's not a bug.
As I've said before, it's just an event of different object.
If you subscribe to user component instead of user identity - overything works as expected.

Btw I wonder where's the best place for subscribing to events. Any idea?
init()? class itself? some external file?..
God is real unless declared as integer
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users