public function login(IdentityInterface $identity, $duration = 0)
    {
        //Using root resolve function for promise conversion
        return resolve(true)
            ->then(function() use (&$identity, $duration) {
                return $this->beforeLogin($identity, false, $duration);
            })->then(function() use (&$identity, $duration) {
                $this->switchIdentity($identity, $duration);
                $id = $identity->getId();
                $ip = $this->request->getUserIP();
                if ($this->enableSession) {
                    $log = "User '$id' logged in from $ip with duration $duration.";
                } else {
                    $log = "User '$id' logged in from $ip. Session not enabled.";
                }

                $this->regenerateCsrfToken();

                if (Reaction::isDebug()) {
                    Reaction::info($log);
                }
                return $this->afterLogin($identity, false, $duration);
            });
    }