Login.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using UnityEngine.UI;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.SceneManagement;
  8. using System;
  9. using System.Security.Cryptography;
  10. using System.Text;
  11. using UnityEngine.Events;
  12. public class Login : MonoBehaviour {
  13. public GameObject username;
  14. public GameObject password;
  15. public Button loginButton;
  16. public Button SwedishButton;
  17. public Button EnglishButton;
  18. public Toggle keepSignedIn;
  19. public Text errorText;
  20. private string Username;
  21. private string Password;
  22. private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
  23. private Color errorColor;
  24. private EventSystem system;
  25. [Serializable]
  26. public class User {
  27. public string userId;
  28. public string pass;
  29. public string salt;
  30. }
  31. private void Start() {
  32. if (Database.Instance.IsKeepSignedIn()) {
  33. SceneManager.LoadScene("MainMenu");
  34. }
  35. loginButton.onClick.AddListener(loginAction);
  36. errorColor = errorText.color;
  37. system = EventSystem.current;
  38. SwedishButton.onClick.AddListener(() => SwitchLanguage(0));
  39. EnglishButton.onClick.AddListener(() => SwitchLanguage(1));
  40. }
  41. private void SwitchLanguage(int langId) {
  42. LocalizationManager.Instance.currentLanguageID = langId;
  43. TextLocalization[] texts = FindObjectsOfType(typeof(TextLocalization)) as TextLocalization[];
  44. foreach (TextLocalization tl in texts) {
  45. tl.UpdateText();
  46. }
  47. InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
  48. foreach (InputFieldLocalization ifl in inputFields) {
  49. ifl.UpdateText();
  50. }
  51. ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
  52. foreach (ButtonLocalization bl in buttonLocale) {
  53. bl.UpdateText();
  54. }
  55. }
  56. private void Update() {
  57. Username = username.GetComponent<InputField>().text;
  58. Password = password.GetComponent<InputField>().text;
  59. if (Input.GetKeyDown(KeyCode.Tab)) {
  60. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  61. if (next != null) {
  62. InputField inputfield = next.GetComponent<InputField>();
  63. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system));
  64. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  65. }
  66. }
  67. }
  68. void loginAction() {
  69. string errorMessage = "";
  70. if (Username == "") {
  71. errorMessage = LocalizationManager.Instance.GetText("REGISTER_ERROR_USERNAME_EMPTY");
  72. }
  73. if (Password == "") {
  74. if (errorMessage != "") {
  75. errorMessage += "\n";
  76. }
  77. errorMessage += LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_EMPTY");
  78. }
  79. if (errorMessage == "") {
  80. errorColor.a = 0;
  81. StartCoroutine(loginCall());
  82. } else {
  83. errorText.text = errorMessage;
  84. errorColor.a = 1;
  85. }
  86. errorText.color = errorColor;
  87. }
  88. IEnumerator loginCall() {
  89. string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username);
  90. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  91. yield return www.SendWebRequest();
  92. if (www.error != null) {
  93. errorText.text = "There was an error logging in " + www.error;
  94. }
  95. string result = www.downloadHandler.text;
  96. User u = new User();
  97. if (!result.Equals("")) {
  98. JsonUtility.FromJsonOverwrite(result, u);
  99. }
  100. if (!u.userId.Equals("")) {
  101. byte[] pwd = Encoding.UTF8.GetBytes(u.salt + Password);
  102. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  103. string pass = Convert.ToBase64String(sha1.ComputeHash(pwd));
  104. if (pass.Equals(u.pass)) {
  105. errorColor.a = 0;
  106. errorText.color = errorColor;
  107. Int32.TryParse(u.userId, out int userId);
  108. Database.Instance.KeepSignedIn(Username, userId, keepSignedIn.isOn);
  109. SceneManager.LoadScene("MainMenu");
  110. } else {
  111. errorText.text = LocalizationManager.Instance.GetText("LOGIN_WRONG_USERNAME_PASSWORD");
  112. errorColor.a = 1;
  113. errorText.color = errorColor;
  114. }
  115. } else {
  116. errorText.text = LocalizationManager.Instance.GetText("LOGIN_USER_NOT_FOUND");
  117. errorColor.a = 1;
  118. errorText.color = errorColor;
  119. }
  120. }
  121. }