public static async Task <bool> ValidateUser(FirebaseUser user, Action <string> successAction, Action <string> errorAction) { if (GameData.IsHeadlessServer) { return(false); } await user.ReloadAsync(); if (!user.IsEmailVerified) { errorAction?.Invoke("Email Not Verified"); return(false); } HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, ServerData.UserFirestoreURL); req.Headers.Add("Authorization", $"Bearer {ServerData.IdToken}"); CancellationToken cancellationToken = new CancellationTokenSource(120000).Token; HttpResponseMessage response; try { response = await ServerData.HttpClient.SendAsync(req, cancellationToken); } catch (Exception e) { Logger.LogError($"Error Accessing Firestore: {e.Message}", Category.DatabaseAPI); errorAction?.Invoke($"Error Accessing Firestore: {e.Message}"); return(false); } string content = await response.Content.ReadAsStringAsync(); FireStoreResponse fr = JsonUtility.FromJson <FireStoreResponse>(content); var newChar = ""; if (fr.fields.character == null) { var newCharacter = new CharacterSettings(); newCharacter.Name = StringManager.GetRandomMaleName(); newCharacter.username = user.DisplayName; newChar = JsonUtility.ToJson(newCharacter); var updateSuccess = await ServerData.UpdateCharacterProfile(newChar); if (!updateSuccess) { Logger.LogError($"Error when updating character", Category.DatabaseAPI); errorAction?.Invoke("Error when updating character"); return(false); } } if (string.IsNullOrEmpty(newChar)) { var characterSettings = JsonUtility.FromJson <CharacterSettings>(Regex.Unescape(fr.fields.character.stringValue)); PlayerPrefs.SetString("currentcharacter", fr.fields.character.stringValue); PlayerManager.CurrentCharacterSettings = characterSettings; } else { PlayerManager.CurrentCharacterSettings = JsonUtility.FromJson <CharacterSettings>(newChar); } successAction?.Invoke("Login success"); PlayerPrefs.SetString("lastLogin", user.Email); PlayerPrefs.Save(); return(true); }
public static async Task <bool> ValidateUser(FirebaseUser user, Action <string> successAction, Action <string> errorAction) { if (GameData.IsHeadlessServer) { return(false); } await user.ReloadAsync(); if (!user.IsEmailVerified) { errorAction?.Invoke("Email Not Verified"); return(false); } HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, ServerData.UserFirestoreURL); req.Headers.Add("Authorization", $"Bearer {ServerData.IdToken}"); CancellationToken cancellationToken = new CancellationTokenSource(120000).Token; HttpResponseMessage response; try { response = await ServerData.HttpClient.SendAsync(req, cancellationToken); } catch (Exception e) { Logger.LogError($"Error Accessing Firestore: {e.Message}", Category.DatabaseAPI); errorAction?.Invoke($"Error Accessing Firestore: {e.Message}"); return(false); } string content = await response.Content.ReadAsStringAsync(); FireStoreResponse fr = JsonUtility.FromJson <FireStoreResponse>(content); FireStoreCharacter fireStoreChar = fr.fields.character; CharacterSettings characterSettings; var settingsValid = false; if (fireStoreChar == null) { // Make a new character since there isn't one in the database characterSettings = new CharacterSettings { Name = StringManager.GetRandomMaleName(), Username = user.DisplayName }; } else { string unescapedJson = Regex.Unescape(fireStoreChar.stringValue); Logger.Log(unescapedJson); try { characterSettings = JsonConvert.DeserializeObject <CharacterSettings>(unescapedJson); } catch (Exception e) { characterSettings = new CharacterSettings(); } // Validate and correct settings in case the customization options change settingsValid = true; } if (!settingsValid) { // Upload the corrected settings so they are valid next time bool updateSuccess = await UpdateCharacterProfile(characterSettings); if (!updateSuccess) { Logger.LogError($"Error when updating character", Category.DatabaseAPI); errorAction?.Invoke("Error when updating character"); return(false); } } // In case PlayerPrefs doesn't already have the settings string jsonChar = JsonConvert.SerializeObject(characterSettings); PlayerPrefs.SetString("currentcharacter", jsonChar); PlayerManager.CurrentCharacterSettings = characterSettings; successAction?.Invoke("Login success"); PlayerPrefs.SetString("lastLogin", user.Email); PlayerPrefs.Save(); return(true); }