页面重定向后Yii2用户身份的损失 [英] Yii2 user identity loss after page redirection
问题描述
我是工作在用户登录的yii2,但不是采用主动纪录还有另一台服务器处理用户的用户名和密码的验证。所以下面是我做过什么来解决:
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. So the following is what I did to work around:
在 LoginForm.php
,我取得了一些变化validatePassword()
其中 apiRest ()
是我用来发送请求,负责验证的远程服务器的方法。
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.
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'];
}
}
而在 LoginForm.php /的getUser()
,我也做了一些调整:
if ($this->_user === false) {
$this->_user = User::createNewUser();
}
return $this->_user;
其中 user.php的
我有:
public static function createNewUser()
{
$user = [
'id' => '',
'username' => '',
'ttl' => '',
'created' => '',
'accessToken' => '',
];
return new static($user);
}
似乎一切都很好,我也可以打印出 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,
]);
}
然而,问题是页重定向后,用户身份消失;有没有在的Yii :: $ APP->用户可>身份
。我错过了什么?请帮我。
However, the problem is after the page redirection, the user identity is gone; there's nothing in Yii::$app->user->identity
. Did I miss anything? Please help me.
推荐答案
检查是否已实施警予\\网络\\ IdentityInterface
在 user.php的
namespace app\models;
use yii\web\IdentityInterface;
class User implements IdentityInterface
{
}
如果是这样的话,一定要检查的Yii :: $ APP-&GT;用户自&GT; enableSession
是 TRUE
(这应该是默认)。
If that's the case, be sure to check that Yii::$app->user->enableSession
is TRUE
(which it should be by default).
这篇关于页面重定向后Yii2用户身份的损失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!