Login.cs 5.1 KB

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