generated from Sithas/conan_template
Почти завершенная регистрация
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <regex>
|
||||
#include <boost/json.hpp>
|
||||
#include <mysqlx/xdevapi.h>
|
||||
#include <mysqlx/common/api.h>
|
||||
@@ -41,7 +42,7 @@ public:
|
||||
{
|
||||
http::response<ResponseType> res{http::status::bad_request, req.version()};
|
||||
|
||||
res.body() = "{ \"detail\": \"cannot deserialize json\"}";
|
||||
res.body() = "{ \"Result\": \"cannot deserialize json\"}";
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
@@ -51,8 +52,26 @@ public:
|
||||
std::string login = req_json.as_object().at("login").as_string().c_str();
|
||||
std::string password = req_json.as_object().at("password").as_string().c_str();
|
||||
|
||||
if (!ValidateLogin(login) || !ValidatePassword(password))
|
||||
{
|
||||
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
|
||||
|
||||
res.body() = "{ \"Result\": \"validations failed\"}";
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
if (user_dao_->GetByLogin(login).has_value())
|
||||
{
|
||||
http::response<ResponseType> res{http::status::conflict, req.version()};
|
||||
|
||||
res.body() = "{ \"Result\": \"user with login " + login + " exists\" }";
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
User user;
|
||||
@@ -61,14 +80,29 @@ public:
|
||||
user.SetPassword(password);
|
||||
|
||||
http::response<ResponseType> res{
|
||||
http::status::ok, req.version()
|
||||
http::status::created, req.version()
|
||||
};
|
||||
|
||||
res.body() = "{ \"detail\": \"ok\"}";
|
||||
res.body() = "{ \"Result\": \"ok\"}";
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
bool ValidateLogin(const std::string& login)
|
||||
{
|
||||
if (login.size() < 3 || login.size() > 50) return false;
|
||||
|
||||
std::regex pattern("[a–z0–9._-]");
|
||||
|
||||
return std::regex_match(login, pattern);
|
||||
}
|
||||
|
||||
bool ValidatePassword(const std::string& password)
|
||||
{
|
||||
return password.size() >= 5;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user