使用“记住我”自动登录无法正常工作

时间:2015-12-24 09:39:28

标签: yii2 yii2-basic-app

我是Yii2的新手。我正在使用Yii 2基本模板。

我在我的网络应用程序上实现了“记住我”功能,但它没有像我认为的那样工作。我可以成功登录(选中“记住我”复选框)。但关闭浏览器并再次打开网站后,我没有登录,而是重定向到登录页面。

我已在配置文件

中将enableAutoLogin设置为true
'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
         'authTimeout' => 86400,
    ],

4 个答案:

答案 0 :(得分:1)

确保您的用户模型已实施yii\web\IdentityInterface并且具有以下方法

  1. findIdentity()
  2. findIdentityByAccessToken()
  3. 的getId()
  4. getAuthKey()
  5. validateAuthKey()

    使用yii \ db \ ActiveRecord;

  6. 使用yii \ web \ IdentityInterface;

    class User extends ActiveRecord implements IdentityInterface
    {
        public static function tableName()
        {
            return 'user';
        }
    
        /**
         * Finds an identity by the given ID.
         *
         * @param string|integer $id the ID to be looked for
         * @return IdentityInterface|null the identity object that matches the given ID.
         */
        public static function findIdentity($id)
        {
            return static::findOne($id);
        }
    
        /**
         * Finds an identity by the given token.
         *
         * @param string $token the token to be looked for
         * @return IdentityInterface|null the identity object that matches the given token.
         */
        public static function findIdentityByAccessToken($token, $type = null)
        {
            return static::findOne(['access_token' => $token]);
        }
    
        /**
         * @return int|string current user ID
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * @return string current user auth key
         */
        public function getAuthKey()
        {
            return $this->auth_key;
        }
    
        /**
         * @param string $authKey
         * @return boolean if auth key is valid for current user
         */
        public function validateAuthKey($authKey)
        {
            return $this->getAuthKey() === $authKey;
        }
    }
    

    有关自动登录的详情,请参阅docs

答案 1 :(得分:1)

1。在您的用户中执行此操作(如果使用任何其他模型进行登录,则使用该模型)。

rules

之前添加此内容

public $rememberMe = true;

并在模型中定义

public function login()
{
     if ($this->validate()) {
         return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
     }
     return false;
}

2。现在在您的视图页面中执行此操作

 <?= $form->field($model, 'rememberMe')->checkbox(['template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>"]) ?>

答案 2 :(得分:0)

登录中使用&#34;记住我&#34;

1 - 使用此链接创建您的身份类:http://www.yiiframework.com/doc-2.0/guide-security-authentication.html

2 - 将此行添加到配置文件中:

...

'components' => [
    'user' => [
        'enableAutoLogin' => true, /* Whether to enable cookie-based login. */
    ],
], 

...

3 - 验证登录表单后使用此代码:

/* If [[enableSession]] is `true`:
- the identity information will be stored in session and be available in the next requests
- in case of `$duration == 0`: as long as the session remains active or till the user closes the browser
- in case of `$duration > 0`: as long as the session remains active or as long as the cookie
  remains valid by it's `$duration` in seconds when [[enableAutoLogin]] is set `true`.
  */
$duration = $this->remember ? (30 * 24 * 3600) : 0;
Yii::$app->user->login($user, $duration);

答案 3 :(得分:-2)

我所做的就是模特:

                if($this->rememberMe == 1){ 
                     setcookie (\Yii::getAlias('@site_title')."_admin_email", $this->username, time()+3600*24*4);
                     setcookie (\Yii::getAlias('@site_title')."_admin_password", $this->password, time()+3600*24*4);
                }else{
                     setcookie (\Yii::getAlias('@site_title')."_admin_email", '');
                     setcookie (\Yii::getAlias('@site_title')."_admin_password", '');
                }

并在视图页面中使用以下方式对其进行检索:

$cookies_email = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_email"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_email"] : '';?>
<?php $cookies_password = isset($_COOKIE[Yii::getAlias('@site_title')."_admin_password"]) ? $_COOKIE[Yii::getAlias('@site_title')."_admin_password"] : '';?>

我的问题解决了:)