From d57e6c34149475a6a15ebf00b30864a2a6e07ba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 29 Sep 2025 18:56:00 +0300 Subject: [PATCH] =?UTF-8?q?Login=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B2=D0=B5?= =?UTF-8?q?=D0=B4=D0=B5=D0=BD=20=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82?= =?UTF-8?q?=D0=B5=D0=BC=D1=83=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20?= =?UTF-8?q?=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/endpoints_handlers/AuthLoginExecutor.h | 84 +++++++++------------- 1 file changed, 33 insertions(+), 51 deletions(-) diff --git a/src/endpoints_handlers/AuthLoginExecutor.h b/src/endpoints_handlers/AuthLoginExecutor.h index f34af02..7694d88 100644 --- a/src/endpoints_handlers/AuthLoginExecutor.h +++ b/src/endpoints_handlers/AuthLoginExecutor.h @@ -10,6 +10,8 @@ #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" #include "../helpers/helpers.h" +#include "../exceptions/exception400_bad_request.h" +#include "../exceptions/exception422_unprocessable_entity.h" namespace uad { @@ -39,65 +41,45 @@ public: const auto body = req.body(); value req_json; - value response_body; - - response_body.emplace_object(); try { req_json = json::parse(body); - - const std::string login = req_json.as_object().at("login").as_string().c_str(); - const std::string password = req_json.as_object().at("password").as_string().c_str(); - - if (login.empty() || password.empty()) - { - http::response res{http::status::unprocessable_entity, req.version()}; - response_body.as_object().emplace("Result", "Login or password are empty"); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; - } - - const std::optional maybe_user = user_dao_->GetByLogin(login); - - if (!maybe_user.has_value() || maybe_user.value().hashed_password != HashPassword(password)) - { - http::response res{http::status::unprocessable_entity, req.version()}; - response_body.as_object().emplace("Result", "Incorrect login or password"); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; - } - const std::string token = GenerateUUID(); - auth_dao_->Login(maybe_user.value().uuid, token); - - http::response res{http::status::ok, req.version()}; - response_body.as_object().emplace("token", token); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; } catch (const system::system_error& err) { - http::response res{http::status::bad_request, req.version()}; - response_body.as_object().emplace("Result", "cannot deserialize json"); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; + throw exception400_bad_request("cannot deserialize json"); } + + + const std::string login = req_json.as_object().at("login").as_string().c_str(); + const std::string password = req_json.as_object().at("password").as_string().c_str(); + + if (login.empty() || password.empty()) + { + throw exception422_unprocessable_entity("Login or password are empty"s); + } + + const std::optional maybe_user = user_dao_->GetByLogin(login); + + if (!maybe_user.has_value() || maybe_user.value().hashed_password != HashPassword(password)) + { + throw exception422_unprocessable_entity("Incorrect login or password"); + } + const std::string token = GenerateUUID(); + auth_dao_->Login(maybe_user.value().uuid, token); + + http::response res{http::status::ok, req.version()}; + value response_body; + + response_body.emplace_object(); + response_body.as_object().emplace("token", token); + + res.body() = serialize(response_body); + res.set(http::field::content_type, "application/json"); + res.content_length(res.body().size()); + + return res; } }; }