[英]Yii2 user identity loss after page redirection
I was working on user login in yii2, but instead of using active record there's another server handle the validation of user's username and password. 我正在yii2中进行用户登录,但是不是使用活动记录,而是由另一台服务器来处理用户名和密码的验证。 So the following is what I did to work around: 因此,以下是我要做的工作:
in LoginForm.php
, I made some changes in validatePassword()
which apiRest()
is a method I used to send request to the remote server in charge of the validation. 在LoginForm.php
,我对validatePassword()
进行了一些更改,其中apiRest()
是一种用于将请求发送到负责验证的远程服务器的方法。
if (!$this->hasErrors()) {
$json = '{
"username": "' . $this->username . '",
"password": "' . $this->password . '"
}';
$response = $this->apiRest("POST", "104.130.126.68:3000/api/users/login", $json);
$user = $this->getUser();
if(!$user || isset($response['error'])) {
$this->addError($attribute, $response['error']['message']);
}
if(isset($response['data'])) {
$user->id = $response['data']['userId'];
$user->username = $this->username;
$user->ttl = $response['data']['ttl'];
$user->created = $response['data']['created'];
$user->accessToken = $response['data']['id'];
}
}
And in the LoginForm.php/getUser()
, I also made some modification: 在LoginForm.php/getUser()
,我还做了一些修改:
if ($this->_user === false) {
$this->_user = User::createNewUser();
}
return $this->_user;
where in User.php
I have: 在User.php
我有:
public static function createNewUser()
{
$user = [
'id' => '',
'username' => '',
'ttl' => '',
'created' => '',
'accessToken' => '',
];
return new static($user);
}
Everything seems fine and I can also print out the user identity in SiteController.php/actionLogin()
: 一切似乎都很好,我还可以在SiteController.php/actionLogin()
打印出用户身份:
if ($model->load(Yii::$app->request->post()) && $model->login()) {
print('<pre>');
print_r(Yii::$app->user->identity->username);
die();
return $this->redirect(['set/index']);
} else {
$this->layout = "plain";
return $this->render('login', [
'model' => $model,
]);
}
However, the problem is after the page redirection, the user identity is gone; 但是,问题是在页面重定向之后,用户标识消失了。 there's nothing in Yii::$app->user->identity
. Yii::$app->user->identity
没有任何内容。 Did I miss anything? 我有想念吗? Please help me. 请帮我。
Check that you have implemented yii\\web\\IdentityInterface
in User.php
检查您是否已在User.php
实现yii\\web\\IdentityInterface
namespace app\models;
use yii\web\IdentityInterface;
class User implements IdentityInterface
{
}
If that's the case, be sure to check that Yii::$app->user->enableSession
is TRUE
(which it should be by default ). 如果是这种情况,请确保检查Yii::$app->user->enableSession
为TRUE
( 默认情况下应为)。
Well, the validation looks fine, however you do this 好吧,验证看起来不错,但是您可以这样做
if(isset($response['data'])) {
$user->id = $response['data']['userId'];
$user->username = $this->username;
$user->ttl = $response['data']['ttl'];
$user->created = $response['data']['created'];
$user->accessToken = $response['data']['id'];
}
but you never set the user back to the model. 但是您永远不会将用户设置回模型。 I believe you should have at the end a 我相信您应该在最后
$this->_user = $user;
Your login probably looks like this 您的登录名可能看起来像这样
/**
* Logs in a user using the provided username and password.
*
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return $this->getUser()->login($this->rememberMe ? 2592000 : 0);
} else {
return false;
}
}
getUser will again return nothing (it will return the empty user you create) as you never actually set it. getUser将再次不返回任何内容(它将返回您创建的空用户),因为您从未真正对其进行过设置。
Also what deacs says I hope it goes without saying :). 也是行长说的话,我希望这不用多说:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.