Skip to content

Instantly share code, notes, and snippets.

@wiverson
Created August 7, 2023 23:18
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/fbb07498743dff19b72c9c58599931e9 to your computer and use it in GitHub Desktop.
Save wiverson/fbb07498743dff19b72c9c58599931e9 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using io.notification;
using Newtonsoft.Json;
using Supabase.Gotrue;
using Supabase.Gotrue.Interfaces;
using UnityEngine;
using static io.notification.NotificationManager.NotificationType;
namespace io.supabase
{
public class UnitySession : IGotrueSessionPersistence<Session>
{
private static string FilePath()
{
const string cacheFileName = "gotrue.cache";
string filePath = Path.Join(Application.persistentDataPath, cacheFileName);
return filePath;
}
public void SaveSession(Session? session)
{
if (session == null)
{
DestroySession();
return;
}
try
{
string filePath = FilePath();
string str = JsonConvert.SerializeObject(session);
using StreamWriter file = new(filePath);
file.Write(str);
file.Dispose();
// NotificationManager.PostMessage(NotificationManager.NotificationType.Debug,
// "Session Saved");
}
catch (Exception e)
{
NotificationManager.PostMessage(NotificationManager.NotificationType.Debug,
"Unable to write cache file.", e);
throw;
}
}
public void DestroySession()
{
string filePath = FilePath();
if (File.Exists(filePath))
{
File.Delete(filePath);
// NotificationManager.PostMessage(NotificationManager.NotificationType.Debug,
// "Session Deleted");
}
}
public Session? LoadSession()
{
string filePath = FilePath();
if (!File.Exists(filePath)) return null;
using StreamReader file = new(filePath);
string sessionJson = file.ReadToEnd();
if (string.IsNullOrEmpty(sessionJson))
{
return null;
}
try
{
return JsonConvert.DeserializeObject<Session>(sessionJson);
}
catch (Exception e)
{
NotificationManager.PostMessage(Auth, "Unable to load user session", e);
return null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment