From 3714207983f244816dd9c8ec12b8ec3554ea7619 Mon Sep 17 00:00:00 2001 From: Vladislav Ostapov Date: Tue, 5 Nov 2024 10:44:36 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D1=8E?= =?UTF-8?q?=D1=89=D0=B8=D0=B9=20logout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/jwt.h | 2 ++ src/main.cpp | 15 ++++++++++++++- static/main.html | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) 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 @@ Мониторинг Настройки Администрирование + Выход