generated from Sithas/conan_template
Рабочая сборка с третьей ручкой
This commit is contained in:
@@ -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());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user