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 JSONChatMessageList GetMessages(JSONUser jsonUser) { if (jsonUser == null) { throw new JJLowPrioException("JSON Objekt darf nicht leer sein!"); } var user = Login(jsonUser.username, jsonUser.password); if (user == null) { throw new JJLowPrioException("Ungültiger Benutzer! Nachrichten werden nicht abgerufen!"); } return(new JSONChatMessageList(user.SentMessages, user.ReceivedMessages)); }
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"); } } }
// POST api/User public JSONResponse Post([FromBody] JSONUser jsonUser) { var response = new JSONResponse(false, null); try { response.responseObject = new JSONUser(_controllerInstance.Register(jsonUser?.username, jsonUser?.password)); } catch (JJLowPrioException jjexc) { response.isException = true; response.responseObject = new JSONJJLowPrioException(jjexc); } catch (Exception exc) { JJChatLoggerInstanceManager.GetInstance().Error("Fatal error occured in 'POST api/User'!", exc); throw exc; } return(response); }