public IEnumerator Register(string username, string password) { JSONUser user = new JSONUser(); user.name = username; user.password = password; var bodyData = JsonUtility.ToJson(user, true); var postData = System.Text.Encoding.UTF8.GetBytes(bodyData); UnityWebRequest request = UnityWebRequest.Post(ip + "/register", UnityWebRequest.kHttpVerbPOST); UploadHandlerRaw uH = new UploadHandlerRaw(postData); request.uploadHandler = uH; request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); if (request.isNetworkError) { Debug.Log(request.error); } else { token = request.downloadHandler.text; PlayerPrefs.SetString("token", token); PlayerPrefs.SetString("user", username); tokenSet = true; LoginEventInfo loginEvent = ScriptableObject.CreateInstance <LoginEventInfo>(); loginEvent.eventState = LoginEventInfo.LoginEventState.REGISTRATION_SUCCESSFUL; NotificationTextScript.GetNotificationTextScript().SetNotificationTextAndShow("Registration successful! The game will start now!"); EventSystem.EventSystem.FireEvent(loginEvent); } }
public void OnLoginClickEvent() { if (usernameInput.text.Length >= 4) { if (passwordInput.text.CompareTo(passwordConfirmInput.text) == 0) { if (passwordInput.text.Length >= 4) { StartCoroutine(APIHandler.getAPIHandler().Register(usernameInput.text, passwordInput.text)); } else { NotificationTextScript.GetNotificationTextScript() .SetNotificationTextAndShow("Passwords is too short!"); } } else { NotificationTextScript.GetNotificationTextScript() .SetNotificationTextAndShow("Passwords do not match!"); } } else { NotificationTextScript.GetNotificationTextScript().SetNotificationTextAndShow("Username is too short!"); } }
private IEnumerator clearData() { UnityWebRequest request = UnityWebRequest.Delete(ip + "/entities"); request.SetRequestHeader("Authorization", token); yield return(request.SendWebRequest()); if (request.isNetworkError) { Debug.Log(request.error); } else { NotificationTextScript.GetNotificationTextScript().SetNotificationTextAndShow("All Data has been cleared successfully"); BackToMenu(); } }
public IEnumerator Login(string username, string password) { JSONUser user = new JSONUser(); user.name = username; user.password = password; var bodyData = JsonUtility.ToJson(user, true); var postData = System.Text.Encoding.UTF8.GetBytes(bodyData); UnityWebRequest request = UnityWebRequest.Post(ip + "/login", UnityWebRequest.kHttpVerbPOST); UploadHandlerRaw uH = new UploadHandlerRaw(postData); request.uploadHandler = uH; request.SetRequestHeader("Content-Type", "application/json"); yield return(request.SendWebRequest()); if (request.isNetworkError) { Debug.Log(request.error); } else { if (!request.downloadHandler.text.Equals("Could not authenticate player")) { token = request.downloadHandler.text; PlayerPrefs.SetString("token", token); PlayerPrefs.SetString("user", username); tokenSet = true; LoginEventInfo loginEvent = ScriptableObject.CreateInstance <LoginEventInfo>(); loginEvent.eventState = LoginEventInfo.LoginEventState.LOGIN_SUCCESSFUL; EventSystem.EventSystem.FireEvent(loginEvent); Debug.Log("Successfully logged in, Token : " + token); } else { NotificationTextScript.GetNotificationTextScript().SetNotificationTextAndShow("Invalid Login Data"); } } }