Skip to content

Instantly share code, notes, and snippets.

@wiverson
Created August 28, 2023 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiverson/23e53b328977d6a8cd0e8665b3147614 to your computer and use it in GitHub Desktop.
Save wiverson/23e53b328977d6a8cd0e8665b3147614 to your computer and use it in GitHub Desktop.
SupabaseManager.cs - example of bootstrapping Supabase in Unity
using System;
using io.notification;
using Supabase;
using Supabase.Gotrue;
using Supabase.Gotrue.Interfaces;
using TMPro;
using UnityEngine;
using static io.notification.NotificationManager;
using static io.notification.NotificationManager.NotificationType;
using Client = Supabase.Client;
using Debug = UnityEngine.Debug;
namespace io.supabase
{
public class SupabaseManager : MonoBehaviour
{
private static SupabaseManager _loaded = null!;
public SupabaseSettings SupabaseSettings = null!;
public NotificationManager NotificationManager = null!;
public TMP_Text email = null!;
private Client? _supabase;
public readonly NetworkStatus NetworkStatus = new();
public Settings? Settings { get; private set; }
public bool Ready { get; private set; }
private async void Start()
{
if (!NotificationManager)
throw new ApplicationException("Missing Notification Manager");
SupabaseOptions options = new();
options.AutoRefreshToken = true;
_supabase = new Client(SupabaseSettings.SupabaseURL, SupabaseSettings.SupabaseAnonKey,
options);
_supabase.Auth.AddDebugListener(DebugListener);
NetworkStatus.Client = (Supabase.Gotrue.Client)_supabase.Auth;
_supabase.Auth.SetPersistence(new UnitySession());
_supabase.Auth.AddStateChangedListener(UnityAuthListener);
_supabase.Auth.LoadSession();
_supabase.Auth.Options.AllowUnconfirmedUserSessions = true;
string url =
$"{SupabaseSettings.SupabaseURL}/auth/v1/settings?apikey={SupabaseSettings.SupabaseAnonKey}";
try
{
_supabase!.Auth.Online = await NetworkStatus.StartAsync(url);
}
catch (NotSupportedException)
{
_supabase!.Auth.Online = true;
}
catch (Exception e)
{
PostMessage(NotificationType.Debug, $"Network Error {e.GetType()}", e);
_supabase!.Auth.Online = false;
}
if (_supabase.Auth.Online)
{
await _supabase.InitializeAsync();
Settings = await _supabase.Auth.Settings();
}
Ready = true;
_loaded = this;
}
public Client? Client()
{
return _supabase;
}
private static void DebugListener(string message, Exception? exception)
{
PostMessage(NotificationType.Debug, message, exception);
Debug.Log(message);
if (exception != null)
{
Debug.Log(exception.Message);
#if !ENABLE_IL2CPP
// Debug.LogException(exception);
#endif
}
}
private void UnityAuthListener(IGotrueClient<User, Session> sender,
Constants.AuthState newState)
{
if (sender.CurrentUser?.Email == null)
email.text = "";
else
{
email.text = sender.CurrentUser.Email;
}
switch (newState)
{
case Constants.AuthState.SignedIn:
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
PostMessage(Auth, "Signed In");
break;
case Constants.AuthState.SignedOut:
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
PostMessage(Auth, "Signed Out");
break;
case Constants.AuthState.UserUpdated:
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
PostMessage(Auth, "Signed In");
break;
case Constants.AuthState.PasswordRecovery:
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
PostMessage(Auth, "Password Recovery");
break;
case Constants.AuthState.TokenRefreshed:
break;
case Constants.AuthState.Shutdown:
break;
default:
Debug.Log($"Unknown Auth State {nameof(newState)}");
break;
}
}
public static SupabaseManager Supabase() { return _loaded; }
private void OnApplicationQuit()
{
if (_supabase != null)
{
_supabase?.Auth.Shutdown();
_supabase = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment