/
Shymer123
/
LumenServer
Обзор
Документация
Войти
/
Shymer123
/
LumenServer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Server/SourceFiles/Controllers/AuthController.cpp
137 строк
5 KB
Shymer123
Initial commit: LumenServer
20 июн 2026, 12:43
20 июн 2026, 12:43
173f14c
Код
Авторство
О чём код?
#include "AuthController.h" #include <iostream> namespace Controllers { AuthController::AuthController(Services::AuthService& service) : m_service(service) {} nlohmann::json AuthController::handleGetAuthData(const nlohmann::json& request) { int32_t request_id = request.value("request_id", -1); std::string phone = request.value("phone", ""); auto salt = m_service.getAuthData(phone); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(salt) { response["status"] = "ok"; response["authData"] = { {"salt", *salt} }; } else { response["status"] = "error"; response["message"] = "User not found"; } return response; } nlohmann::json AuthController::handleLogin(const nlohmann::json& request) { int32_t request_id = request.value("request_id", -1); std::string phone = request.value("phone", ""); std::string password = request.value("password", ""); std::string device_id = request.value("device_id", ""); auto result = m_service.login(phone, password, device_id); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(result) { response["status"] = "ok"; response["access_token"] = result->tokens.access_token; response["refresh_token"] = result->tokens.refresh_token; response["device_id"] = device_id; response["user_id"] = result->user_id; } else { response["status"] = "error"; response["message"] = "Invalid credentials"; } return response; } nlohmann::json AuthController::handleRegister(const nlohmann::json& request) { int32_t request_id = request.value("request_id", -1); std::string display_name = request.value("display_name", ""); std::string password = request.value("password", ""); std::string salt = request.value("salt", ""); std::string phone = request.value("phone", ""); std::string device_id = request.value("device_id", ""); auto result = m_service.registerUser(display_name, password, salt, phone, device_id); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(result) { response["status"] = "ok"; response["access_token"] = result->tokens.access_token; response["refresh_token"] = result->tokens.refresh_token; response["device_id"] = device_id; response["user_id"] = result->user_id; } else { response["status"] = "error"; response["message"] = "User already exists"; } return response; } nlohmann::json AuthController::handleAutoLogin(const nlohmann::json& request) { int32_t request_id = request.value("request_id", -1); std::string refreshToken = request.value("refresh_token", ""); std::string device_id = request.value("device_id", ""); auto result = m_service.autoLogin(refreshToken, device_id); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(result) { response["status"] = "ok"; response["access_token"] = result->tokens.access_token; response["refresh_token"] = result->tokens.refresh_token; response["device_id"] = device_id; response["user_id"] = result->user_id; } else { response["status"] = "error"; response["message"] = "Invalid refresh token"; } return response; } nlohmann::json AuthController::handleLogout(const Core::Network::RequestContext& ctx) { if(const auto& jsonData = ctx.json()) { const auto& json = jsonData->get(); int32_t request_id = json.value("request_id", -1); std::string deviceId = json.value("device_id", ""); bool result = m_service.logoutDevice(*ctx.userId, deviceId); nlohmann::json response; response["request_id"] = static_cast<int64_t>(request_id); if(result) { response["status"] = "ok"; } else { response["status"] = "error"; response["message"] = "Couldn't log out"; } return response; } return {{"status", "error"}, {"message", "The request is not a json request"}}; } } // namespace Controllers