pica/handler/register.cpp

61 lines
1.7 KiB
C++
Raw Normal View History

2023-12-14 22:17:28 +00:00
// SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "register.h"
2023-12-20 22:42:13 +00:00
#include "server/server.h"
Handler::Register::Register(Server* server):
Handler("register", Request::Method::post),
server(server)
2023-12-14 22:17:28 +00:00
{}
void Handler::Register::handle(Request& request) {
std::map form = request.getForm();
2023-12-20 22:42:13 +00:00
std::map<std::string, std::string>::const_iterator itr = form.find("login");
if (itr == form.end())
return error(request, Result::noLogin);
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);
}
2023-12-14 22:17:28 +00:00
2023-12-20 22:42:13 +00:00
Response res(request);
nlohmann::json body = nlohmann::json::object();
body["result"] = Result::success;
res.setBody(body);
res.send();
}
2023-12-14 22:17:28 +00:00
2023-12-20 22:42:13 +00:00
void Handler::Register::error(Request& request, Result result) {
2023-12-14 22:17:28 +00:00
Response res(request);
nlohmann::json body = nlohmann::json::object();
2023-12-20 22:42:13 +00:00
body["result"] = result;
2023-12-14 22:17:28 +00:00
res.setBody(body);
res.send();
}