Register.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Networking;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.SceneManagement;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System;
  10. public class Register : MonoBehaviour {
  11. public GameObject username;
  12. public GameObject password;
  13. public GameObject email;
  14. public Text errorText;
  15. public Button registerButton;
  16. private string Username;
  17. private string Password;
  18. private string Email;
  19. private string form;
  20. private string registerUserUrl = "http://nordh.xyz/narKampen/dbFiles/Register.php?";
  21. private Color errorColor;
  22. private EventSystem system;
  23. // Start is called before the first frame update
  24. void Start() {
  25. registerButton.onClick.AddListener(RegisterAction);
  26. system = EventSystem.current;
  27. errorColor = errorText.color;
  28. }
  29. // Update is called once per frame
  30. void Update() {
  31. Username = username.GetComponent<InputField>().text;
  32. Password = password.GetComponent<InputField>().text;
  33. Email = email.GetComponent<InputField>().text;
  34. if (Input.GetKeyDown(KeyCode.Tab)) {
  35. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  36. if (next != null) {
  37. InputField inputfield = next.GetComponent<InputField>();
  38. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  39. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  40. }
  41. }
  42. }
  43. void RegisterAction() {
  44. string errorMessage = "";
  45. if (Username == "" || Username.Length < 3) {
  46. errorMessage = "Username is obligatory and needs to be atleast 3 characters long";
  47. }
  48. if (Password == "") {
  49. if (errorMessage != "") {
  50. errorMessage += "\n";
  51. }
  52. errorMessage += "Password is obligatory";
  53. }
  54. if (Email == "") {
  55. if (errorMessage != "") {
  56. errorMessage += "\n";
  57. }
  58. errorMessage += "Email is obligatory";
  59. }
  60. if (errorMessage != "") {
  61. errorText.text = errorMessage;
  62. errorColor.a = 1;
  63. } else {
  64. errorColor.a = 0;
  65. StartCoroutine(RegisterUser());
  66. }
  67. errorText.color = errorColor;
  68. }
  69. IEnumerator RegisterUser() {
  70. // TODO register user at server, check for already in use for username and email.
  71. // TODO - Save OS and device id
  72. string deviceId = SystemInfo.deviceUniqueIdentifier;
  73. string os = SystemInfo.operatingSystem;
  74. string salt = GetSalt();
  75. byte[] pwd = Encoding.UTF8.GetBytes(salt + Password);
  76. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  77. byte[] pass = sha1.ComputeHash(pwd);
  78. string postUrl = registerUserUrl + "name=" + UnityWebRequest.EscapeURL(Username) + "&password=" + UnityWebRequest.EscapeURL(Convert.ToBase64String(pass)) + "&email=" + UnityWebRequest.EscapeURL(Email) + "&s=" + UnityWebRequest.EscapeURL(salt) + "&did=" + deviceId + "&os=" + UnityWebRequest.EscapeURL(os);
  79. Debug.Log(postUrl);
  80. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  81. yield return www.SendWebRequest();
  82. if (www.error != null) {
  83. errorText.text = "There was an error registering this user, try again later: " + www.error;
  84. errorColor.a = 1;
  85. errorText.color = errorColor;
  86. } else {
  87. string result = www.downloadHandler.text;
  88. if (int.TryParse(www.downloadHandler.text, out int userId)) {
  89. errorColor.a = 0;
  90. errorText.color = errorColor;
  91. PlayerPrefs.SetInt("UserId", userId);
  92. SceneManager.LoadScene("MainMenu");
  93. } else {
  94. errorText.text = "Failed to register, username/email already exists";
  95. errorColor.a = 1;
  96. errorText.color = errorColor;
  97. }
  98. }
  99. }
  100. private string GetSalt() {
  101. RNGCryptoServiceProvider rnd = new RNGCryptoServiceProvider();
  102. int saltLength = 32;
  103. byte[] salt = new byte[saltLength];
  104. rnd.GetNonZeroBytes(salt);
  105. return Convert.ToBase64String(salt);
  106. }
  107. }