/// <summary> /// Method for loading loggedUser based on player prefs content /// </summary> /// <returns> /// Was user succesfully logged from prefabs /// </returns> public bool LoginUserFromPlayerPrefs() { //Generating string from player prefs var jsonString = PlayerPrefs.GetString(loggedUserLabel, null); //if json is not null - user exists in player prefs if (!String.IsNullOrEmpty(jsonString)) { //Generating json data from string var jsonData = UserLoader.GetUserJSONDataFromString(jsonString); //Generating user from json data var user = UserLoader.GenerateUserFromJSONData(jsonData, this); UserLoader._loggedUser = user; Translator.SetLanguageFromLoggedUser(); return(true); } else { //if json is null - there is no user in player prefs - set currently logged user as null UserLoader._loggedUser = null; return(false); } }
/// <summary> /// Method for getting user data from server /// </summary> /// <param name="jwt"> /// JWT of currently logged user /// </param> /// <returns> /// user data as JSON (string) /// </returns> public static async Task <UserJSONData> GetUserDataFromServer(string jwt) { //timeout = 10 sec using (var webClient = new WebclientWithTimeout(10 * 1000)) { webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; webClient.Headers["x-auth-token"] = jwt; var testJSONUserData = await webClient.DownloadStringTaskWithTimeoutAsync(UserService.userApiURL); var userJsonObject = UserLoader.GetUserJSONDataFromString(testJSONUserData); return(userJsonObject); } }
/// <summary> /// Method for getting user data from server /// </summary> /// <param name="email"> /// Email of user /// </param> /// <param name="password"> /// User's password /// </param> /// <returns> /// user data as JSON (string) /// return null in case id or password is invalid /// </returns> public static async Task <UserJSONData> GetUserDataFromServer(string email, string password) { //timeout = 10 sec using (var webClient = new WebclientWithTimeout(10 * 1000)) { //Creating Object to parse to JSON content based on given credentials var newAuthObject = new AuthJSONData(); newAuthObject.email = email; newAuthObject.password = password; //Generating JSON content var jsonAuthObject = JsonUtility.ToJson(newAuthObject); webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; var testJSONUserData = await webClient.UploadStringTaskWithTimeoutAsync(UserService.authApiURL, jsonAuthObject); var userJsonObject = UserLoader.GetUserJSONDataFromString(testJSONUserData); return(userJsonObject); } }