protected function loginByCookie()
    {
        return $this->getIdentityAndDurationFromCookie()
            ->then(function($data) {
                if (isset($data['identity'], $data['duration'])) {
                    /** @var IdentityInterface $identity */
                    $identity = $data['identity'];
                    $duration = $data['duration'];
                    return $this->beforeLogin($identity, true, $duration)
                        ->then(function() use (&$identity, $duration) {
                            $this->switchIdentity($identity, $this->autoRenewCookie ? $duration : 0);
                            $id = $identity->getId();
                            $ip = $this->request->getUserIP();
                            Reaction::info("User '$id' logged in from $ip via cookie.");
                            return $this->afterLogin($identity, true, $duration);
                        });
                } else {
                    return reject(new Error("Invalid cookie value or user identity"));
                }
            });
    }