protected function logout()
    {
        $oldUser = $this->user;
        $this->user = new User();

        // Removes all session data (and invalidates all CSRF tokens)
        $this->session->invalidate();

        $this->triggerEvent("afterLogout", [$oldUser]);
        
        // After logout redirect to main page
        // If user was on restricted page, 403 would appear right after logout
        $this->redirectToPage($this->config["afterLogout"]);
    }