#pragma once #include #include #include #include "IExecutor.h" #include "../DAO/IAuthDAO.h" #include "../exceptions/session_exception.h" namespace uad { template class AuthLogoutExecutor : public IExecutor { mysqlx::Session& session_; const std::shared_ptr& auth_dao_; public: AuthLogoutExecutor(mysqlx::Session& session, const std::shared_ptr& auth_dao) : session_(session), auth_dao_(auth_dao) { } boost::beast::http::response operator ()( boost::beast::http::request>&& req ) override { using namespace boost; using namespace boost::json; using namespace boost::beast; using namespace std::string_literals; BOOST_LOG_TRIVIAL(info) << "POST /api/v1/Auth/Logout - Request"; const auto body = req.body(); value req_json; try { req_json = json::parse(body); } catch (const system::system_error& err) { BOOST_LOG_TRIVIAL(error) << "POST /api/v1/Auth/Logout - Response 500: Cannot deserialize json"; throw session_exception(http::status::internal_server_error, "cannot deserialize json"s); } const std::string token = req_json.as_object().at("token").as_string().c_str(); if (!auth_dao_->Logout(token)) { BOOST_LOG_TRIVIAL(error) << "POST /api/v1/Auth/Logout - Response 400: Token is not authorized"; throw session_exception(http::status::bad_request, "Token is not authorized"s); } http::response res{http::status::ok, req.version()}; res.body() = "null"s; res.set(http::field::content_type, "application/json"); res.content_length(res.body().size()); return res; } }; }