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($id) {
$identity = static::findOne($id);
if (!isset($identity)) {
$identity = static::findOne(['oAuthId' => $id]);
}
return $identity;
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null) {
return static::findOne(['accesstoken' => $token]);
}
/**
* @inheritdoc
*/
public function getId() {
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey() {
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey) {
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password) {
return $this->password === $password;
}
public function beforeSave($insert) {
if (parent::beforeSave($insert)) {
if ($this->isNewRecord) {
$helper = new Security();
$this->authkey = $helper->generateRandomString();
}
return true;
}
return false;
}
////////////////////////////////////////////////////////
.....
}