/// <summary> /// Loads session from prefs /// Called automatically in constructor. /// </summary> public void Load() { if (PlayerPrefs.HasKey(PrefKey)) { Account = JsonSerializer.Deserialize<AccountDetails>(PlayerPrefs.GetString(PrefKey)); } else { Account = new AccountDetails { Id = Guid.NewGuid().ToString() }; } if (PlayerPrefs.HasKey(FBPrefKey)) { FBToken = JsonSerializer.Deserialize<AccessToken>(PlayerPrefs.GetString(FBPrefKey)); } }
/// <summary> /// Requests a new guest account. Use for 'Skip Sign In' option. /// </summary> /// <returns></returns> public void Guest(Action<Response> callback) { // Is Authenticated if (IsAuthenticated) { if (Application.internetReachability == NetworkReachability.NotReachable) { callback(new Response()); return; } //Refresh HttpPostAsync<AccountDetails>("Get", o => { //reload details if (o.IsSuccess && o.Result != null) { ReadDetails(o.Result); } }); return; } if (Account == null || string.IsNullOrEmpty(Account.Id)) { Account = new AccountDetails { Id = Guid.NewGuid().ToString() }; } //No Internet ? if (Application.internetReachability == NetworkReachability.NotReachable) { callback(new Response(new Exception("Not signed in, no internet."))); return; } //Save serverside in background HttpPostAsync<AccountDetails>("Guest", new AccountGuestSignIn { UserId = Account.Id, }, o => { if (o.IsSuccess && o.Result != null) { ReadDetails(o.Result); } else { Debug.LogException(o.Exception); //sign out HttpService.ClearSession(); HttpService.SaveSession(); } callback(new Response()); }); }
void ReadDetails(AccountDetails model) { Account = model; Save(); }
/// <summary> /// Sign out and clear cache /// </summary> public void SignOut() { HttpService.ClearSession(); HttpService.SaveSession(); Account = new AccountDetails { Id = Guid.NewGuid().ToString() }; Save(); }