first what so ever registration

This commit is contained in:
Blue 2023-12-20 19:42:13 -03:00
parent 0c50cfa639
commit 99a9fd507e
Signed by: blue
GPG key ID: 9B203B252A63EE38
17 changed files with 285 additions and 25 deletions

View file

@ -3,20 +3,57 @@
#include "register.h"
Handler::Register::Register():
Handler("register", Request::Method::post)
#include "server/server.h"
Handler::Register::Register(Server* server):
Handler("register", Request::Method::post),
server(server)
{}
void Handler::Register::handle(Request& request) {
std::map form = request.getForm();
std::map<std::string, std::string>::const_iterator itr = form.find("login");
if (itr == form.end())
return error(request, Result::noLogin);
std::cout << "Received form:" << std::endl;
for (const auto& pair : form)
std::cout << '\t' << pair.first << ": " << pair.second << std::endl;
const std::string& login = itr->second;
if (login.empty())
return error(request, Result::emptyLogin);
//TODO login policies checkup
itr = form.find("password");
if (itr == form.end())
return error(request, Result::noPassword);
const std::string& password = itr->second;
if (password.empty())
return error(request, Result::emptyPassword);
//TODO password policies checkup
try {
server->registerAccount(login, password);
} catch (const std::exception& e) {
std::cerr << "Exception on registration:\n\t" << e.what() << std::endl;
return error(request, Result::unknownError);
} catch (...) {
std::cerr << "Unknown exception on registration" << std::endl;
return error(request, Result::unknownError);
}
Response res(request);
nlohmann::json body = nlohmann::json::object();
body["result"] = "ok";
body["result"] = Result::success;
res.setBody(body);
res.send();
}
void Handler::Register::error(Request& request, Result result) {
Response res(request);
nlohmann::json body = nlohmann::json::object();
body["result"] = result;
res.setBody(body);
res.send();

View file

@ -5,12 +5,30 @@
#include "handler.h"
class Server;
namespace Handler {
class Register : public Handler::Handler {
public:
Register();
Register(Server* server);
virtual void handle(Request& request);
enum class Result {
success,
noLogin,
emptyLogin,
loginExists,
loginPolicyViolation,
noPassword,
emptyPassword,
passwordPolicyViolation,
unknownError
};
private:
void error(Request& request, Result result);
private:
Server* server;
};
}