RizzeBattleRoyale/Assets/Editor/SupabaseSecretsVerifier.cs
2025-10-15 20:28:33 +04:00

54 lines
2.1 KiB
C#

#if UNITY_EDITOR
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
using Supabase;
public static class SupabaseSecretsVerifier
{
[MenuItem("Tools/Supabase/Verify Secrets (Editor)")]
public static void VerifyMenu() { _ = VerifyAsync(); }
static string Mask(string s) =>
string.IsNullOrEmpty(s) ? "<empty>" :
(s.Length <= 8 ? $"***{s[^4..]}" : $"{s[..4]}***{s[^6..]}");
static async Task VerifyAsync()
{
try
{
// 1) Load secrets (dev file in Editor OR Resources if present)
var sec = await SupabaseSecretsLoader.LoadAsync();
// 2) Log masked values
Debug.Log($"[Supabase] URL: {sec.url}\nAnon: {Mask(sec.anonKey)}");
// 3) Try Client init (no network calls needed here)
var opts = new SupabaseOptions { AutoConnectRealtime = false, AutoRefreshToken = true };
var client = new Client(sec.url, sec.anonKey, opts);
await client.InitializeAsync();
// 4) Optional lightweight API ping (Auth health -> 200 OK)
var healthUrl = $"{sec.url.TrimEnd('/')}/auth/v1/health";
using var req = UnityWebRequest.Get(healthUrl);
req.SetRequestHeader("apikey", sec.anonKey);
await req.SendWebRequest();
var ok = req.result == UnityWebRequest.Result.Success && req.responseCode == 200;
var msg = ok ? "✅ Secrets OK: Editor can init Supabase and reach /auth/v1/health."
: $"⚠️ Init OK but health probe returned {req.responseCode} ({req.error}). " +
"This can be blocked by firewall; init still proves secrets are loaded.";
Debug.Log(msg);
EditorUtility.DisplayDialog("Supabase Secrets", msg + $"\n\nHost: {new System.Uri(sec.url).Host}\nAnon: {Mask(sec.anonKey)}", "OK");
}
catch (System.Exception ex)
{
Debug.LogError($"❌ Supabase secrets verification failed: {ex.Message}");
EditorUtility.DisplayDialog("Supabase Secrets", "❌ Verification failed:\n" + ex.Message, "OK");
}
}
}
#endif