to have a better understanding of login process, i tried to user the following example and put it in the view to ease of test:
class testUser implements IdentityInterface{
public $username = 'ali';
public static function findIdentity($id)
{
return 'findIdentity';
}
public static function findIdentityByAccessToken($token, $type = null)
{
return 'findIdentityByAccessToken';
}
public function getId()
{
return 'getId';
}
public function getAuthKey()
{
return 'getAuthKey';
}
public function validateAuthKey($authKey)
{
return 'validateAuthKey';
}
}
$ob = new testUser();
echo (Yii::$app->user->isGuest) ? 'is guest</br>' : 'is user</br>';
Yii::$app->user->login($ob, 3600);
echo (Yii::$app->user->isGuest) ? 'is guest</br>' : 'is user</br>';
the problem is the user can not retain logged in, by refreshing the page, always shows "is guest" and then "is user"
i change the following cofing:
'components' => [
'user' => [
// 'identityClass' => 'common\models\User',
'identityClass' => 'frontend\models\TestUser',
'enableAutoLogin' => true,
],
....
and the TestClass is:
<?php
namespace frontend\models;
use yii\web\IdentityInterface;
use Yii;
class TestUser implements IdentityInterface{
public $username = 'ali';
public static function findIdentity($id)
{
return 'findIdentity';
}
public static function findIdentityByAccessToken($token, $type = null)
{
return 'findIdentityByAccessToken';
}
public function getId()
{
return 'getId';
}
public function getAuthKey()
{
return 'getAuthKey';
}
public function validateAuthKey($authKey)
{
return 'validateAuthKey';
}
}
but get this error:
the error occurred in this line:
echo (Yii::$app->user->isGuest) ? 'is guest</br>' : 'is user</br>';
the TestUser impelement the IdentityInterface, so why i got that error?
thanks
the exception is before login process
i tried to login and then the user is guest or not, no exception occurred:
Yii::$app->user->login($ob);
echo (Yii::$app->user->isGuest) ? 'is guest</br>' : 'is user</br>';
but the user session didnt remain and think destroy by refresh
any help?
RedDog
(P Y)
March 4, 2015, 1:56pm
4
This is because method findIdentity() must return the identity object that matches the given ID, in your case it returns the string.
what should i return?
if return true is enough?
is there an example to show use another model instead of default advanced template user login/register from scratch?
thanks
RedDog
(P Y)
March 4, 2015, 4:45pm
6
[quote=desatir7316]if return true is enough?
[/quote]
no
[quote=desatir7316]what should i return?
[/quote]
an object that implements the identity interface
[quote=desatir7316]is there an example to show use another model instead of default advanced template user login/register from scratch?
[/quote]
Yes, basic template, which doesn’t use AR for user indentity class.
thanks, every thing is fine
i have two more questions, thanks in response
where and how(implicit/explicit) findIdentity will call and how $id will pass to it?
if I call \Yii::$app->user->loign(object, duration) and pass an object that implements identityInterface , will the user be login?
if yes, why didnt work for me?
thanks for your time
understand this one, id will pass to it via getId() method
RedDog
(P Y)
March 4, 2015, 9:51pm
9
for test login process, i tried this code in basic template, site/index:
echo \Yii::$app->user->isGuest ? 'is Guest</br>' : 'is User</br>' ;
$user = new \app\models\User();
\Yii::$app->user->login($user, 1000);
echo \Yii::$app->user->isGuest ? 'is Guest</br>' : 'is User</br>' ;
return;
by it didnt logged me in for 1000 seconds, by refreshing always echos following:
is Guest
is User
i want to logged in directly and without any authentication to have a better understanding of this process
the login method need an object that implements IdentityInterface, so i passed this object to it, what is wrong with me?
thanks in advanced
RedDog
(P Y)
March 5, 2015, 8:44am
11
One more time, You didn’t implement IdentityInterface in your TestUser class. What you did is not the implementation. Read the documentation for IdentityInterface and pay particular attention to what methods should return.
For a better understanding explore the code of the class yii\web\User https://github.com/yiisoft/yii2/blob/master/framework/web/User.php