diff --git a/src/auth/jwt.h b/src/auth/jwt.h index 0456ab4..6559356 100644 --- a/src/auth/jwt.h +++ b/src/auth/jwt.h @@ -6,6 +6,8 @@ namespace http::auth::jwt { extern std::string secretKey; + constexpr const char* EMPTY_AUTH_COOKIE = "auth=;Path=/; Max-Age=86400; HttpOnly; SameSite=Lax";; + void generateSecretKey(); /** diff --git a/src/main.cpp b/src/main.cpp index 1a199df..1b82354 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -128,7 +128,12 @@ public: s.resources.emplace_back(std::make_unique("/login", [this](const auto& req, auto& rep) { if (req.method == "GET") { - sf->serve(LOGIN_HTML, rep); + auto user = auth.getSession(req); + if (user == nullptr) { + sf->serve(LOGIN_HTML, rep); + } else { + http::server::httpRedirect(rep, "/"); + } } else if (req.method == "POST") { rep.status = http::server::ok; rep.headers.clear(); @@ -153,6 +158,14 @@ public: http::server::stockReply(http::server::bad_request, rep); } })); + s.resources.emplace_back(std::make_unique("/logout", [this](const auto& req, auto& rep) { + if (req.method == "GET") { + http::server::httpRedirect(rep, "/"); + rep.headers.push_back({.name = "Set-Cookie", .value = http::auth::jwt::EMPTY_AUTH_COOKIE}); + } else { + http::server::stockReply(http::server::bad_request, rep); + } + })); s.resources.emplace_back(std::make_unique("/favicon.ico", [this](const auto& req, auto& rep) { boost::ignore_unused(req); sf->serve(FAVICON_ICO, rep); })); s.resources.emplace_back(std::make_unique("/images/krokodil_vzryvaetsya_hd.gif", [this](const auto& req, auto& rep) { boost::ignore_unused(req); sf->serve(KROKODIL_GIF, rep); })); diff --git a/static/main.html b/static/main.html index 0639e3b..5ec834a 100644 --- a/static/main.html +++ b/static/main.html @@ -22,6 +22,7 @@ Мониторинг Настройки Администрирование + Выход