Cleaned up and commented version.
This commit is contained in:
@@ -8,6 +8,9 @@ using Client = Supabase.Client;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using TMPro;
|
||||
|
||||
// Dieses Script wickelt die gesamte Kommunikation mit der Datenbank ab. Das Skript enthält noch einige Verbindungstests (z.B. zu Google), die bei Bedarf zum Debuggen verwendet
|
||||
// werden können.
|
||||
|
||||
namespace GhostSystem
|
||||
{
|
||||
public class SupabaseManager : MonoBehaviour
|
||||
@@ -36,7 +39,7 @@ namespace GhostSystem
|
||||
|
||||
private async UniTask Initialize()
|
||||
{
|
||||
if (instance == null)
|
||||
if (instance == null) // Zuerst wird sichergestellt, dass nur eine Instanz des Supabase-Managers existiert und beim Szenenwechsel mitgenommen wird.
|
||||
{
|
||||
instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
@@ -47,29 +50,29 @@ namespace GhostSystem
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("🌐 Starting Supabase setup...");
|
||||
await RunDiagnostics();
|
||||
Debug.Log("Starting Supabase setup...");
|
||||
await RunDiagnostics(); // Dann wird geprüft, ob die Supabase-Instanz erreichbar ist.
|
||||
|
||||
await InitSupabaseAsync();
|
||||
OnSuperbaseReady?.Invoke();
|
||||
await InitSupabaseAsync(); // Eigentlicher Verbindungsaufbau.
|
||||
OnSuperbaseReady?.Invoke(); // Wenn erfolgreich eine Verbindung zu Supabase aufgebaut wurde, wird das als Event gemeldet.
|
||||
}
|
||||
|
||||
private async UniTask InitSupabaseAsync()
|
||||
private async UniTask InitSupabaseAsync() // Wird aufgerufen, um die Verbindung zu Supabase zu initialisieren.
|
||||
{
|
||||
if (supabase != null)
|
||||
return;
|
||||
|
||||
string path = Path.Combine(Application.persistentDataPath, authFileName);
|
||||
|
||||
if (File.Exists(path))
|
||||
if (File.Exists(path)) // Ist bereits eine Datei mit Email und Passwort auf dem VR-Headset hinterlegt, wird diese ausgelesen.
|
||||
{
|
||||
string[] lines = await UniTask.Run(() => File.ReadAllLines(path));
|
||||
email = lines[0];
|
||||
password = lines[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
email = string.IsNullOrEmpty(SystemInfo.deviceUniqueIdentifier)
|
||||
{ // Falls nicht, werden neue Anmeldedaten erstellt.
|
||||
email = string.IsNullOrEmpty(SystemInfo.deviceUniqueIdentifier) // Fake-Email-Adresse wird aus der GUID des Gerätes generiert.
|
||||
? $"{Guid.NewGuid()}@device.local" // Fallback, generiert theoretisch bei jeder Neuinstallation der App einen anderen User.
|
||||
: $"{SystemInfo.deviceUniqueIdentifier}@device.local";
|
||||
|
||||
@@ -78,7 +81,7 @@ namespace GhostSystem
|
||||
await UniTask.Run(() => File.WriteAllLines(path, new[] { email, password }));
|
||||
}
|
||||
|
||||
Debug.Log($"📧 Using Email: {email}, Password: {password}");
|
||||
Debug.Log($"Using Email: {email}, Password: {password}");
|
||||
|
||||
supabase = new Client(subabaseUrl, supabaseAnonKey);
|
||||
|
||||
@@ -86,39 +89,39 @@ namespace GhostSystem
|
||||
{
|
||||
await supabase.InitializeAsync();
|
||||
textbox.text += "\n\rSupabase initialized.";
|
||||
Debug.Log("✅ Supabase initialized.");
|
||||
Debug.Log("Supabase initialized.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"❌ Initialization failed: {ex}");
|
||||
Debug.LogError($"Initialization failed: {ex}");
|
||||
textbox.text += "\n\rInitialization failed";
|
||||
}
|
||||
|
||||
await AuthenticateAsync(email, password);
|
||||
Debug.Log($"👤 Authenticated user ID: {userId}");
|
||||
Debug.Log($"Authenticated user ID: {userId}");
|
||||
}
|
||||
|
||||
public async UniTask AuthenticateAsync(string email, string password)
|
||||
public async UniTask AuthenticateAsync(string email, string password) // Diese Methode übernimmt die Anmeldung als User bei der Supabase-Instanz.
|
||||
{
|
||||
try
|
||||
{
|
||||
await supabase.Auth.SignIn(email, password);
|
||||
await supabase.Auth.SignIn(email, password); // Nutzt die übergebene Mail-Adresse und das Passwort, um den Unser anzumelden.
|
||||
textbox.text += "\n\rSign-in successful";
|
||||
Debug.Log("🔓 Sign-in successful");
|
||||
Debug.Log("Sign-in successful");
|
||||
}
|
||||
catch (Exception authEx)
|
||||
{
|
||||
catch (Exception authEx) // Funktioniert die Anmeldung nicht, wird versucht, einen neuen User mit der übergebenen Mail-Adresse und dem
|
||||
{ // Passwort zu registrieren.
|
||||
Debug.LogWarning($"Sign-in failed, trying sign-up: {authEx.Message}");
|
||||
try
|
||||
{
|
||||
await supabase.Auth.SignUp(email, password);
|
||||
textbox.text += "\n\rSign-up successful";
|
||||
Debug.Log("🆕 Sign-up successful");
|
||||
Debug.Log("Sign-up successful");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
textbox.text += "\n\rAuthentication failed";
|
||||
Debug.LogError($"❌ Authentication failed: {ex}");
|
||||
Debug.LogError($"Authentication failed: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,7 +131,7 @@ namespace GhostSystem
|
||||
await UniTask.WhenAll(CheckSupabaseHealthAsync(), CheckGoogleConnectionAsync());
|
||||
}
|
||||
|
||||
private async UniTask CheckSupabaseHealthAsync()
|
||||
private async UniTask CheckSupabaseHealthAsync() // Prüft, ob Supabase erreichbar ist.
|
||||
{
|
||||
using var www = UnityWebRequest.Get($"{subabaseUrl}/auth/v1/health");
|
||||
www.SetRequestHeader("apikey", supabaseAnonKey);
|
||||
@@ -137,16 +140,16 @@ namespace GhostSystem
|
||||
if (www.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
//textbox.text += $"\n\rSupabase health: {www.downloadHandler.text}";
|
||||
Debug.Log($"✅ Supabase health: {www.downloadHandler.text}");
|
||||
Debug.Log($"Supabase health: {www.downloadHandler.text}");
|
||||
}
|
||||
else
|
||||
{
|
||||
//textbox.text += $"\n\rSupabase health check failed: {www.error}";
|
||||
Debug.LogError($"❌ Supabase health check failed: {www.error}");
|
||||
Debug.LogError($"Supabase health check failed: {www.error}");
|
||||
}
|
||||
}
|
||||
|
||||
private async UniTask CheckGoogleConnectionAsync()
|
||||
private async UniTask CheckGoogleConnectionAsync() // Testet, ob grundsätzlich eine Internetverbindung besteht.
|
||||
{
|
||||
using var www = UnityWebRequest.Get("https://www.google.com");
|
||||
await www.SendWebRequest();
|
||||
@@ -154,12 +157,12 @@ namespace GhostSystem
|
||||
if (www.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
//textbox.text += "\n\rGoogle request succeeded.";
|
||||
Debug.Log("✅ Google request succeeded.");
|
||||
Debug.Log("Google request succeeded.");
|
||||
}
|
||||
else
|
||||
{
|
||||
textbox.text += $"\n\rGoogle connection failed: {www.error}";
|
||||
Debug.LogError($"❌ Google connection failed: {www.error}");
|
||||
Debug.LogError($"Google connection failed: {www.error}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user