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,689
  • 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,689
  • 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 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 105
  • 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,689
  • 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

#9 User is offline   pjcarly 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 27-July 14

Posted 27 July 2014 - 04:37 PM

I managed to implement this by setting my configuration file for the user component as follows
'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'on afterLogin' => function($event)
            {
                Yii::$app->user->identity->afterLogin($event);
            }
        ],


I also added a field LastLoginDate to the database,
and in my User model (which implemented the IdentityInterface), I added this function:

public function afterLogin($event)
    {
        $this->LastLoginDate = gmdate("Y-m-d H:i:s");
        $this->save();
    }

0

#10 User is offline   Monitor Backlinks 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 04-August 14

Posted 06 August 2014 - 10:38 AM

I think this is the right case to use behavior:

<?php

use yii\db\Expression;

class User extends ActiveRecord implements IdentityInterface
{
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    \yii\web\User::EVENT_AFTER_LOGIN => ['last_login_timestamp'],
                ],
                'value' => new Expression('NOW()'),
            ],
        ];
    }
}


instead of extending `afterLogin()`.
0

#11 User is offline   Stefano Mtangoo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 188
  • Joined: 02-January 12
  • Location:Dar es Salaam, Tanzania

Posted 06 August 2014 - 05:11 PM

View PostORey, on 24 November 2013 - 08:32 AM, said:


Btw I wonder where's the best place for subscribing to events. Any idea?
init()? class itself? some external file?..

Same question here!
And God said, Let there be science...and it was so!
Training Manager Written in Yii here
0

#12 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 14 August 2014 - 07:20 AM

Best solutions is here, follow this wiki -

How to get & store Last Login DateTime in database

http://www.yiiframew...me-in-database/
Thanks & regards,

ROHIT SUTHAR,
Mumbai(India)

Follow me - @rohisuthar

www.yiiframework.us



Don't be shellfish to click +1 button
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