Created
August 7, 2023 23:18
-
-
Save wiverson/fbb07498743dff19b72c9c58599931e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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