diff --git a/WebApplication/Application.cpp b/WebApplication/Application.cpp index 27ed0c7..4498353 100644 --- a/WebApplication/Application.cpp +++ b/WebApplication/Application.cpp @@ -59,12 +59,13 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl LOG(info) << "relative resources url: " << relativeResourcesUrl(); LOG(info) << "resources url: " << resourcesUrl(); LOG(info) << "internal path: " << internalPath(); - internalPathChanged().connect(this, &Application::handlePathChange); - handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath); auto app = Amass::Singleton::instance(); m_loginPage = std::make_unique(app->authService(), m_session->users(), m_session->login()); m_loginPage->processEnvironment(); + + handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath); + internalPathChanged().connect(this, &Application::handlePathChange); } Application::~Application() { @@ -75,22 +76,43 @@ void Application::authEvent() { const Wt::Auth::User &u = m_session->login().user(); LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")" << " logged in."; + if (m_loginPage) { + m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage)); + } else if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) { + m_loginPage = m_loginPageRef->parent()->removeWidget(m_loginPageRef); + m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage)); + } + setInternalPath("/", true); } else { + m_loginPage = m_navigationBar->removeLoginItem(); LOG(info) << "User logged out."; } } void Application::handlePathChange(const std::string &path) { LOG(info) << "handlePathChange: " << path; - if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) { - m_root->clear(); - // m_loginPageRef = m_root->addWidget(std::move(m_loginPage)); - } else if (path.starts_with("/wt/visitor/analysis")) { - m_root->clear(); - m_root->addNew(*m_session); + if (path.starts_with("/wt/login")) { + if (m_session->login().loggedIn()) { + std::exit(0); + } else { + if (m_loginPage) { + m_root->clear(); + m_loginPageRef = m_root->addWidget(std::move(m_loginPage)); + } + } } else { - m_root->clear(); - m_root->addNew(); + if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) { + m_loginPage = m_root->removeWidget(m_loginPageRef); + } + + if (path.starts_with("/wt/register")) { + } else if (path.starts_with("/wt/visitor/analysis")) { + m_root->clear(); + m_root->addNew(*m_session); + } else { + m_root->clear(); + m_root->addNew(); + } } } diff --git a/WebApplication/NavigationBar.cpp b/WebApplication/NavigationBar.cpp index d1d574f..bbc82b5 100644 --- a/WebApplication/NavigationBar.cpp +++ b/WebApplication/NavigationBar.cpp @@ -1,4 +1,7 @@ #include "NavigationBar.h" +#include "LoginPage.h" +#include +#include #include constexpr auto Burger = R"( @@ -8,19 +11,17 @@ constexpr auto Burger = R"( )"; +constexpr auto Login = R"( +${register-button class="bulma-button bulma-is-primary"} +${login-button class="bulma-button bulma-is-light"} +)"; + constexpr auto Menu = R"(
- + ${login-item}
)"; @@ -46,7 +47,35 @@ NavigationBar::NavigationBar() : Wt::WTemplate(Template) { setAttributeValue("aria-label", "main navigation"); auto navbarBurger = bindWidget("navbar-burger", std::make_unique(Burger)); navbarBurger->setHtmlTagName("a"); - auto navbarMenu = bindWidget("navbar-menu", std::make_unique(Menu)); - navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id())); + m_menu = bindWidget("navbar-menu", std::make_unique(Menu)); + createLoginButtons(); + + navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), m_menu->id())); +} + +NavigationBar::~NavigationBar() { +} + +LoginPage *NavigationBar::addLoginItem(std::unique_ptr item) { + auto ret = m_menu->bindWidget("login-item", std::move(item)); + ret->addStyleClass("bulma-navbar-item"); + return ret; +} + +std::unique_ptr NavigationBar::removeLoginItem() { + auto ret = m_menu->removeWidget("login-item"); + createLoginButtons(); + return std::unique_ptr(dynamic_cast(ret.release())); +} + +void NavigationBar::createLoginButtons() { + auto loginItem = m_menu->bindWidget("login-item", std::make_unique(Login)); + loginItem->addStyleClass("bulma-buttons"); + auto loginButton = loginItem->bindWidget("login-button", std::make_unique("登录")); + loginButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/login", true); }); + + auto registerButton = loginItem->bindWidget("register-button", std::make_unique("注册")); + registerButton->setTextFormat(Wt::TextFormat::XHTML); + registerButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/register", true); }); } diff --git a/WebApplication/NavigationBar.h b/WebApplication/NavigationBar.h index d54e147..d5968e7 100644 --- a/WebApplication/NavigationBar.h +++ b/WebApplication/NavigationBar.h @@ -3,9 +3,20 @@ #include -class NavigationBar: public Wt::WTemplate { +class LoginPage; + +class NavigationBar : public Wt::WTemplate { public: NavigationBar(); + ~NavigationBar(); + LoginPage *addLoginItem(std::unique_ptr item); + std::unique_ptr removeLoginItem(); + +protected: + void createLoginButtons(); + +private: + Wt::WTemplate *m_menu = nullptr; }; #endif // __NAVIGATIONBAR_H__ \ No newline at end of file diff --git a/resources/auth_css_theme.xml b/resources/auth_css_theme.xml index 27b44fc..556c700 100644 --- a/resources/auth_css_theme.xml +++ b/resources/auth_css_theme.xml @@ -218,9 +218,8 @@ -
- ${user-name} ${logout} -
+ ${user-name} + ${logout}