I
I
Ivan Efimov2018-09-14 01:03:14
Yii
Ivan Efimov, 2018-09-14 01:03:14

Unable to update, recreate auth_key cookie in yii2?

why doesn't this switchIdentity() method recreate the auth_key? and this removeIdentityCookie() method won't remove cookies? what am I doing wrong?
it is necessary that in the future, after closing the browser, users can again automatically log in using a cookie, while they can delete all sessions (sessions) on other devices, except for the current one, how can this be done?
SiteController.php

/**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {

        // `identity` текущего пользователя. `Null`, если пользователь не аутентифицирован.
        $identity = Yii::$app->user->identity;
        // var_dump($identity);
        // beauty_view($identity);


        // switchIdentity($identity, $duration = 0)
        // Yii::$app->user->switchIdentity($identity, 0);
        // if( (bool)Yii::$app->request->get('delete_all_sessions') === true) Yii::$app->user->logout();

        if( (bool)Yii::$app->request->get('delete_all_sessions') === true) Yii::$app->user->switchIdentity($identity, 0);
        if( (bool)Yii::$app->request->get('delete_all_sessions') === true) Yii::$app->user->removeIdentityCookie();

        var_dump(Yii::$app->user->getId);

       // Yii::$app->getResponse()->getCookies()->remove(Yii::createObject(array_merge($this->identityCookie, [
       //      'class' => 'yii\web\Cookie',
       //  ])));


        // beauty_view(Yii::$app);
        return $this->render('index');
    }

config
'user' => [
        'identityClass' => 'app\models\User', // User must implement the IdentityInterface
        // 'identityClass' => 'common\models\User', // User must implement the IdentityInterface
        'enableAutoLogin' => true,
        'enableSession' => true
        // 'loginUrl' => ['user/login'],
        // ...
    ],

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-09-14
@webinar

I don’t know what a miracle you are trying to do, but here is what I noticed from the errors:
1. Yii::$app->user->getIdreturns the user ID, and getIdentity() returns an object, so I don’t understand the surprise that removeIdentityCookie() does not affect the id in any way.
Also with switchIdentity - this method changes Identity without changing the user.
2. if you want to change user, you should call logiout() and then login() for another user.
I highly recommend looking at this object: https://www.yiiframework.com/doc/api/2.0/yii-web-user

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question