-- In a LocalScript or ModuleScript
local AethexAuth = {}
local API_BASE = "https://aethex.dev/api"
function AethexAuth:authenticate(playerId, playerName)
local body = game:GetService("HttpService"):JSONEncode({
game = "roblox",
player_id = tostring(playerId),
player_name = playerName,
platform = "PC"
})
local response = game:GetService("HttpService"):PostAsync(
API_BASE .. "/games/game-auth",
body,
Enum.HttpContentType.ApplicationJson
)
local data = game:GetService("HttpService"):JSONDecode(response)
return data
end
function AethexAuth:verifyToken(sessionToken)
local response = game:GetService("HttpService"):PostAsync(
API_BASE .. "/games/verify-token",
game:GetService("HttpService"):JSONEncode({
session_token = sessionToken,
game = "roblox"
}),
Enum.HttpContentType.ApplicationJson
)
return game:GetService("HttpService"):JSONDecode(response)
end
return AethexAuth
local Players = game:GetService("Players")
local AethexAuth = require(game.ServerScriptService:WaitForChild("AethexAuth"))
Players.PlayerAdded:Connect(function(player)
local authResult = AethexAuth:authenticate(player.UserId, player.Name)
if authResult.success then
player:SetAttribute("AethexSessionToken", authResult.session_token)
player:SetAttribute("AethexUserId", authResult.user_id)
print("Player " .. player.Name .. " authenticated with AeThex")
else
print("Authentication failed:", authResult.error)
end
end)
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class AethexAuth : MonoBehaviour
{
private const string API_BASE = "https://aethex.dev/api";
[System.Serializable]
public class AuthResponse
{
public bool success;
public string session_token;
public string user_id;
public string username;
public int expires_in;
public string error;
}
public static IEnumerator AuthenticatePlayer(
string playerId,
string playerName,
System.Action<AuthResponse> callback)
{
var request = new UnityWebRequest(
$"{API_BASE}/games/game-auth",
"POST"
);
var requestBody = new AuthRequest
{
game = "unity",
player_id = playerId,
player_name = playerName,
platform = "PC"
};
string jsonBody = JsonUtility.ToJson(requestBody);
request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonBody));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var response = JsonUtility.FromJson<AuthResponse>(request.downloadHandler.text);
callback(response);
}
else
{
callback(new AuthResponse { error = request.error });
}
}
[System.Serializable]
private class AuthRequest
{
public string game;
public string player_id;
public string player_name;
public string platform;
}
}
public class GameManager : MonoBehaviour
{
void Start()
{
string playerId = SystemInfo.deviceUniqueIdentifier;
string playerName = "UnityPlayer_" + Random.Range(1000, 9999);
StartCoroutine(AethexAuth.AuthenticatePlayer(playerId, playerName, OnAuthComplete));
}
void OnAuthComplete(AethexAuth.AuthResponse response)
{
if (response.success)
{
Debug.Log($"Authenticated as {response.username}");
// Store token for future requests
PlayerPrefs.SetString("AethexSessionToken", response.session_token);
PlayerPrefs.SetString("AethexUserId", response.user_id);
}
else
{
Debug.LogError($"Auth failed: {response.error}");
}
}
}