yii\web\User::getIdentityAndDurationFromCookie PHP Method

getIdentityAndDurationFromCookie() protected method

This method is used when [[enableAutoLogin]] is true. This method attempts to authenticate a user using the information in the identity cookie.
See also: loginByCookie()
Since: 2.0.9
protected getIdentityAndDurationFromCookie ( ) : array | null
return array | null Returns an array of 'identity' and 'duration' if valid, otherwise null.
    protected function getIdentityAndDurationFromCookie()
    {
        $value = Yii::$app->getRequest()->getCookies()->getValue($this->identityCookie['name']);
        if ($value === null) {
            return null;
        }
        $data = json_decode($value, true);
        if (count($data) == 3) {
            list($id, $authKey, $duration) = $data;
            /* @var $class IdentityInterface */
            $class = $this->identityClass;
            $identity = $class::findIdentity($id);
            if ($identity !== null) {
                if (!$identity instanceof IdentityInterface) {
                    throw new InvalidValueException("{$class}::findIdentity() must return an object implementing IdentityInterface.");
                } elseif (!$identity->validateAuthKey($authKey)) {
                    Yii::warning("Invalid auth key attempted for user '{$id}': {$authKey}", __METHOD__);
                } else {
                    return ['identity' => $identity, 'duration' => $duration];
                }
            }
        }
        $this->removeIdentityCookie();
        return null;
    }