Login.cs 5.3 KB

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