Yii::$App->User->Identity Value Is Not Persistent Across Controllers

Hi,

I am here facing an issue in using Yii AuthClient that badly need your expertise and help.

Basically, the object Yii::$app->user->identity seems not be persistent. I can see the value is set within the same controller where I call login(). But, when it jumps into a different controller, Yii::$app->user->identity became null.

I have tried search for a solution online but unfotunately cannot find the right answer to my problem. Appreciate your help.

The following are the files and the main code in them.

=========== SiteController.php ===========

public function successCallback($client) {


    $attributes = $client->getUserAttributes();





    $oAuthType = $client->getId();


    $oAuthId = $attributes['id'];


    $email = $attributes['email'];


    $firstName = $attributes['first_name'];


    $lastName = $attributes['last_name'];


    $verified = $attributes['verified'];





    // user login or signup comes here


    if ($verified) {


        $provider = new ActiveDataProvider([


            'query' => User::find()->where('oAuthId="' . $oAuthId . '" AND oAuthType="' . $oAuthType . '"'),


        ]);





        $now = new \DateTime;


        if (count($provider->getModels()) === 0) {


            $user = new User();


            $user->username = $email;


            $user->oAuthType = $oAuthType;


            $user->oAuthId = $oAuthId;


            // $user->accesstoken = $client->getAccessToken();


            $user->firstName = $firstName;


            $user->lastName = $lastName;


            $user->status = 1;


            $user->dateTimeLastSignedIn = $now->format('Y-m-d H:i:s');


            $user->dateTimeSignedup = $now->format('Y-m-d H:i:s');


            $user->save();


        } else {


            $user = $provider->getModels()[0];


            $user->dateTimeLastSignedIn = $now->format('Y-m-d H:i:s');


            $user->save();


        }


    }





    $model = new LoginFormWithFB();


    $result = $model->login($oAuthId);


    $isGuest = Yii::$app->user->isGuest;


    $identity = Yii::$app->user->identity;


}

===============User.php===============

<?php

namespace app\models;

use Yii;

use yii\data\ActiveDataProvider;

use yii\base\Security;

/**

  • This is the model class for table "user".

  • @property string $id

  • @property string $username

  • @property string $password

  • @property integer $role

  • @property string $oAuthType

  • @property string $oAuthId

  • @property string $accesstoken

  • @property string $authkey

  • @property string $firstName

  • @property string $lastName

  • @property string $profilePicture

  • @property integer $status

  • @property string $familyId

  • @property string $friendCircleId

  • @property string $dateTimeSignedup

  • @property string $dateTimeLastSignedIn

  • @property Game[] $games

  • @property Gameinstance[] $gameinstances

  • @property Gameoftheday[] $gameofthedays

  • @property Gameplaned[] $gameplaneds

  • @property Gamerecommended[] $gamerecommendeds

  • @property Kid[] $ks

  • @property FriendCircle $friendCircle

  • @property Family $family

*/

class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {

/**


 * @inheritdoc


 */


public static function findIdentity(&#036;id) {


    &#036;identity = static::findOne(&#036;id);


    if (&#33;isset(&#036;identity)) {


        &#036;identity = static::findOne(['oAuthId' =&gt; &#036;id]);


    }





    return &#036;identity;


}





/**


 * @inheritdoc


 */


public static function findIdentityByAccessToken(&#036;token, &#036;type = null) {


    return static::findOne(['accesstoken' =&gt; &#036;token]);


}





/**


 * @inheritdoc


 */


public function getId() {


    return &#036;this-&gt;id;


}





/**


 * @inheritdoc


 */


public function getAuthKey() {


    return &#036;this-&gt;authKey;


}





/**


 * @inheritdoc


 */


public function validateAuthKey(&#036;authKey) {


    return &#036;this-&gt;authKey === &#036;authKey;


}





/**


 * Validates password


 *


 * @param  string  &#036;password password to validate


 * @return boolean if password provided is valid for current user


 */


public function validatePassword(&#036;password) {


    return &#036;this-&gt;password === &#036;password;


}





public function beforeSave(&#036;insert) {


    if (parent::beforeSave(&#036;insert)) {


        if (&#036;this-&gt;isNewRecord) {


            &#036;helper = new Security();


            &#036;this-&gt;authkey = &#036;helper-&gt;generateRandomString();


        }


        return true;


    }


    return false;


}





////////////////////////////////////////////////////////


.....

}