#pragma once #include #include #include #include #include #include "IExecutor.h" #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" #include "../helpers/helpers.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; const auto body = req.body(); value req_json; try { req_json = json::parse(body); } catch (const system::system_error& err) { 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)) { 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; } }; }