Рабочая сборка с третьей ручкой

This commit is contained in:
Антон
2025-09-27 12:06:12 +03:00
parent 353ff528a8
commit d4c01cd70c
2 changed files with 10 additions and 29 deletions
+9 -28
View File
@@ -17,14 +17,12 @@ template <class Body, class Allocator, class ResponseType>
class AuthLogoutExecutor : public IExecutor<Body, Allocator, ResponseType>
{
mysqlx::Session& session_;
const std::shared_ptr<IUserDAO>& user_dao_;
const std::shared_ptr<IAuthDAO>& auth_dao_;
public:
AuthLogoutExecutor(mysqlx::Session& session,
const std::shared_ptr<IUserDAO>& user_dao,
const std::shared_ptr<IAuthDAO>& auth_dao)
: session_(session), user_dao_(user_dao), auth_dao_(auth_dao)
: session_(session), auth_dao_(auth_dao)
{
}
@@ -39,49 +37,32 @@ public:
auto body = req.body();
value req_json;
value response_body;
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();
const std::string token = req_json.as_object().at("token").as_string().c_str();
if (login.empty() || password.empty())
if (!auth_dao_->Logout(token))
{
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
response_body.as_object().emplace("Result", "Login or password are empty");
http::response<ResponseType> res{http::status::bad_request, req.version()};
res.body() = serialize(response_body);
response_body.as_object().emplace("Result", "token is not authorized");
res.body() = json::serialize(response_body);
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res;
}
std::optional<User> maybe_user = user_dao_->GetByLogin(login);
if (!maybe_user.has_value() || (maybe_user.value().GetHashedPassword() != HashPassword(password)))
{
http::response<ResponseType> 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;
}
auto token = GenerateUUID();
auth_dao_->Login(maybe_user.value().GetUUID(), token);
http::response<ResponseType> res{http::status::ok, req.version()};
response_body.as_object().emplace("token", token);
res.body() = serialize(response_body);
res.body() = "true"s;
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
+1 -1
View File
@@ -60,7 +60,7 @@ public:
routes_pathes_["/api/v1/Auth/Logout"] = std::make_unique<RouteController>(
typename RouteController::HTTPMethodsToExecutors{
{boost::beast::http::verb::post,
std::make_shared<RouteAuthLogoutExecutor>(session_, user_dao_, auth_dao_)}
std::make_shared<RouteAuthLogoutExecutor>(session_, auth_dao_)}
}
);
}