public static IEnumerator DoLogin3rd(string username, string user3rdid, string source, string token, string image, System.Action <bool> onFinish) { Login3rd user = new Login3rd(); user.username = username; user.user3rdid = user3rdid; user.token = token; user.source = source; user.image = image; string data = JsonConvert.SerializeObject(user); UnityWebRequest request = UnityWebRequest.Post(host + "/login3rd", ""); request.SetRequestHeader("Content-Type", "application/json"); if (data != null) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data); UploadHandlerRaw upHandler = new UploadHandlerRaw(bytes); upHandler.contentType = "application/json"; request.uploadHandler = upHandler; } yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { onFinish(true); } else { string tk = request.downloadHandler.text; SignInResult result = JsonConvert.DeserializeObject <SignInResult>(tk); PlayerPrefHandler.SaveString(PlayerPrefHandler.TOKEN, result.token); onFinish(false); } }
public static IEnumerator DoLogin(string username, string password, System.Action <bool> onLoginFinish) { LoginRegister login = new LoginRegister(); login.username = username; login.password = password; string data = JsonConvert.SerializeObject(login); Debug.Log(data); UnityWebRequest request = UnityWebRequest.Post(host + "/login", ""); request.SetRequestHeader("Content-Type", "application/json"); if (data != null) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data); UploadHandlerRaw upHandler = new UploadHandlerRaw(bytes); upHandler.contentType = "application/json"; request.uploadHandler = upHandler; } yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { onLoginFinish(true); } else { string token = request.downloadHandler.text; SignInResult result = JsonConvert.DeserializeObject <SignInResult>(token); PlayerPrefHandler.SaveString(PlayerPrefHandler.TOKEN, result.token); onLoginFinish(false); } }
public static IEnumerator RefreshToken(string token, System.Action <bool> onFinish) { UnityWebRequest request = UnityWebRequest.Get(host + "/refresh-token"); request.SetRequestHeader("Content-Type", "application/json"); request.SetRequestHeader("Authorization", "Bearer " + token); yield return(request.SendWebRequest()); if (request.isNetworkError || request.isHttpError) { onFinish(true); } else { string newToken = request.downloadHandler.text; SignInResult result = JsonConvert.DeserializeObject <SignInResult>(newToken); PlayerPrefHandler.SaveString(PlayerPrefHandler.TOKEN, result.token); onFinish(false); } }