174 lines
5.4 KiB
C++
174 lines
5.4 KiB
C++
/**************************************************************************/
|
|
/* aethex_auth.cpp */
|
|
/**************************************************************************/
|
|
/* AeThex Engine */
|
|
/* https://aethex.dev */
|
|
/**************************************************************************/
|
|
|
|
#include "aethex_auth.h"
|
|
#include "aethex_cloud.h"
|
|
#include "core/io/http_client.h"
|
|
|
|
void AethexAuth::_bind_methods() {
|
|
ClassDB::bind_method(D_METHOD("login", "email", "password"), &AethexAuth::login);
|
|
ClassDB::bind_method(D_METHOD("register_account", "username", "email", "password"), &AethexAuth::register_account);
|
|
ClassDB::bind_method(D_METHOD("get_profile"), &AethexAuth::get_profile);
|
|
ClassDB::bind_method(D_METHOD("logout"), &AethexAuth::logout);
|
|
|
|
ClassDB::bind_method(D_METHOD("login_async", "email", "password", "callback"), &AethexAuth::login_async);
|
|
ClassDB::bind_method(D_METHOD("register_async", "username", "email", "password", "callback"), &AethexAuth::register_async);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_logged_in"), &AethexAuth::is_logged_in);
|
|
ClassDB::bind_method(D_METHOD("get_user_id"), &AethexAuth::get_user_id);
|
|
ClassDB::bind_method(D_METHOD("get_username"), &AethexAuth::get_username);
|
|
ClassDB::bind_method(D_METHOD("get_email"), &AethexAuth::get_email);
|
|
|
|
ADD_SIGNAL(MethodInfo("login_success", PropertyInfo(Variant::DICTIONARY, "user")));
|
|
ADD_SIGNAL(MethodInfo("login_failed", PropertyInfo(Variant::STRING, "error")));
|
|
ADD_SIGNAL(MethodInfo("logged_out"));
|
|
}
|
|
|
|
AethexAuth::AethexAuth() {
|
|
}
|
|
|
|
AethexAuth::~AethexAuth() {
|
|
}
|
|
|
|
void AethexAuth::set_cloud(AethexCloud *p_cloud) {
|
|
cloud = p_cloud;
|
|
}
|
|
|
|
Dictionary AethexAuth::login(const String &p_email, const String &p_password) {
|
|
Dictionary result;
|
|
result["success"] = false;
|
|
|
|
if (!cloud) {
|
|
result["error"] = "Cloud not initialized";
|
|
return result;
|
|
}
|
|
|
|
Dictionary data;
|
|
data["email"] = p_email;
|
|
data["password"] = p_password;
|
|
|
|
Dictionary response = cloud->make_request("/api/auth/login", HTTPClient::METHOD_POST, data);
|
|
|
|
if (response.get("success", false)) {
|
|
Dictionary resp_data = response.get("data", Dictionary());
|
|
|
|
if (resp_data.has("token")) {
|
|
cloud->set_auth_token(resp_data["token"]);
|
|
|
|
if (resp_data.has("user")) {
|
|
Dictionary user = resp_data["user"];
|
|
user_id = user.get("id", "");
|
|
username = user.get("username", "");
|
|
email = user.get("email", "");
|
|
logged_in = true;
|
|
|
|
emit_signal("login_success", user);
|
|
}
|
|
|
|
result["success"] = true;
|
|
result["user"] = resp_data.get("user", Dictionary());
|
|
}
|
|
} else {
|
|
String error = response.get("error", "Login failed");
|
|
emit_signal("login_failed", error);
|
|
result["error"] = error;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
Dictionary AethexAuth::register_account(const String &p_username, const String &p_email, const String &p_password) {
|
|
Dictionary result;
|
|
result["success"] = false;
|
|
|
|
if (!cloud) {
|
|
result["error"] = "Cloud not initialized";
|
|
return result;
|
|
}
|
|
|
|
Dictionary data;
|
|
data["username"] = p_username;
|
|
data["email"] = p_email;
|
|
data["password"] = p_password;
|
|
|
|
Dictionary response = cloud->make_request("/api/auth/register", HTTPClient::METHOD_POST, data);
|
|
|
|
if (response.get("success", false)) {
|
|
result["success"] = true;
|
|
result["data"] = response.get("data", Dictionary());
|
|
} else {
|
|
result["error"] = response.get("error", "Registration failed");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
Dictionary AethexAuth::get_profile() {
|
|
Dictionary result;
|
|
result["success"] = false;
|
|
|
|
if (!cloud) {
|
|
result["error"] = "Cloud not initialized";
|
|
return result;
|
|
}
|
|
|
|
Dictionary response = cloud->make_request("/api/auth/profile", HTTPClient::METHOD_GET);
|
|
|
|
if (response.get("success", false)) {
|
|
result["success"] = true;
|
|
result["user"] = response.get("data", Dictionary());
|
|
} else {
|
|
result["error"] = response.get("error", "Failed to get profile");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void AethexAuth::logout() {
|
|
if (cloud) {
|
|
cloud->set_auth_token("");
|
|
}
|
|
|
|
user_id = "";
|
|
username = "";
|
|
email = "";
|
|
logged_in = false;
|
|
|
|
emit_signal("logged_out");
|
|
}
|
|
|
|
void AethexAuth::login_async(const String &p_email, const String &p_password, const Callable &p_callback) {
|
|
// TODO: Implement proper async
|
|
Dictionary result = login(p_email, p_password);
|
|
if (p_callback.is_valid()) {
|
|
p_callback.call(result);
|
|
}
|
|
}
|
|
|
|
void AethexAuth::register_async(const String &p_username, const String &p_email, const String &p_password, const Callable &p_callback) {
|
|
// TODO: Implement proper async
|
|
Dictionary result = register_account(p_username, p_email, p_password);
|
|
if (p_callback.is_valid()) {
|
|
p_callback.call(result);
|
|
}
|
|
}
|
|
|
|
bool AethexAuth::is_logged_in() const {
|
|
return logged_in;
|
|
}
|
|
|
|
String AethexAuth::get_user_id() const {
|
|
return user_id;
|
|
}
|
|
|
|
String AethexAuth::get_username() const {
|
|
return username;
|
|
}
|
|
|
|
String AethexAuth::get_email() const {
|
|
return email;
|
|
}
|