generated from Sithas/conan_template
Login переведен на систему исключений вместо возврата
This commit is contained in:
@@ -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<ResponseType> 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<user> maybe_user = user_dao_->GetByLogin(login);
|
||||
|
||||
if (!maybe_user.has_value() || maybe_user.value().hashed_password != 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;
|
||||
}
|
||||
const std::string token = GenerateUUID();
|
||||
auth_dao_->Login(maybe_user.value().uuid, token);
|
||||
|
||||
http::response<ResponseType> 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<ResponseType> 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<user> 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<ResponseType> 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user