Login.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // CheckFirebaseGoogleDependencies();
  33. if (Database.Instance.IsKeepSignedIn()) {
  34. SceneManager.LoadScene("MainMenu");
  35. }
  36. loginButton.onClick.AddListener(loginAction);
  37. errorColor = errorText.color;
  38. system = EventSystem.current;
  39. SwedishButton.onClick.AddListener(() => SwitchLanguage(0));
  40. EnglishButton.onClick.AddListener(() => SwitchLanguage(1));
  41. }
  42. private void SwitchLanguage(int langId) {
  43. LocalizationManager.Instance.currentLanguageID = langId;
  44. TextLocalization[] texts = FindObjectsOfType(typeof(TextLocalization)) as TextLocalization[];
  45. foreach (TextLocalization tl in texts) {
  46. tl.UpdateText();
  47. }
  48. InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
  49. foreach (InputFieldLocalization ifl in inputFields) {
  50. ifl.UpdateText();
  51. }
  52. ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
  53. foreach (ButtonLocalization bl in buttonLocale) {
  54. bl.UpdateText();
  55. }
  56. }
  57. private void Update() {
  58. Username = username.GetComponent<InputField>().text;
  59. Password = password.GetComponent<InputField>().text;
  60. if (Input.GetKeyDown(KeyCode.Tab)) {
  61. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  62. if (next != null) {
  63. InputField inputfield = next.GetComponent<InputField>();
  64. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system));
  65. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  66. }
  67. }
  68. }
  69. void loginAction() {
  70. string errorMessage = "";
  71. if (Username == "") {
  72. errorMessage = LocalizationManager.Instance.GetText("REGISTER_ERROR_USERNAME_EMPTY");
  73. }
  74. if (Password == "") {
  75. if (errorMessage != "") {
  76. errorMessage += "\n";
  77. }
  78. errorMessage += LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_EMPTY");
  79. }
  80. if (errorMessage == "") {
  81. errorColor.a = 0;
  82. StartCoroutine(loginCall());
  83. } else {
  84. errorText.text = errorMessage;
  85. errorColor.a = 1;
  86. }
  87. errorText.color = errorColor;
  88. }
  89. IEnumerator loginCall() {
  90. string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username);
  91. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  92. yield return www.SendWebRequest();
  93. if (www.error != null) {
  94. errorText.text = "There was an error logging in " + www.error;
  95. }
  96. string result = www.downloadHandler.text;
  97. User u = new User();
  98. if (!result.Equals("")) {
  99. JsonUtility.FromJsonOverwrite(result, u);
  100. }
  101. if (u.userId != null && !u.userId.Equals("")) {
  102. byte[] pwd = Encoding.UTF8.GetBytes(u.salt + Password);
  103. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  104. string pass = Convert.ToBase64String(sha1.ComputeHash(pwd));
  105. if (pass.Equals(u.pass)) {
  106. errorColor.a = 0;
  107. errorText.color = errorColor;
  108. Int32.TryParse(u.userId, out int userId);
  109. Database.Instance.KeepSignedIn(Username, userId, keepSignedIn.isOn);
  110. SceneManager.LoadScene("MainMenu");
  111. } else {
  112. errorText.text = LocalizationManager.Instance.GetText("LOGIN_WRONG_USERNAME_PASSWORD");
  113. errorColor.a = 1;
  114. errorText.color = errorColor;
  115. }
  116. } else {
  117. errorText.text = LocalizationManager.Instance.GetText("LOGIN_USER_NOT_FOUND");
  118. errorColor.a = 1;
  119. errorText.color = errorColor;
  120. }
  121. }
  122. }