Login.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Text errorText;
  19. private string Username;
  20. private string Password;
  21. private string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
  22. private Color errorColor;
  23. private EventSystem system;
  24. [Serializable]
  25. public class User {
  26. public string userId;
  27. public string pass;
  28. public string salt;
  29. }
  30. private void Start() {
  31. loginButton.onClick.AddListener(loginAction);
  32. errorColor = errorText.color;
  33. system = EventSystem.current;
  34. SwedishButton.onClick.AddListener(() => SwitchLanguage(0));
  35. EnglishButton.onClick.AddListener(() => SwitchLanguage(1));
  36. }
  37. private void SwitchLanguage(int langId) {
  38. LocalizationManager.Instance.currentLanguageID = langId;
  39. TextLocalization[] texts = FindObjectsOfType(typeof(TextLocalization)) as TextLocalization[];
  40. foreach (TextLocalization tl in texts) {
  41. tl.UpdateText();
  42. }
  43. InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
  44. foreach (InputFieldLocalization ifl in inputFields) {
  45. ifl.UpdateText();
  46. }
  47. ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
  48. foreach (ButtonLocalization bl in buttonLocale) {
  49. bl.UpdateText();
  50. }
  51. }
  52. private void Update() {
  53. Username = username.GetComponent<InputField>().text;
  54. Password = password.GetComponent<InputField>().text;
  55. if (Input.GetKeyDown(KeyCode.Tab)) {
  56. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  57. if (next != null) {
  58. InputField inputfield = next.GetComponent<InputField>();
  59. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  60. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  61. }
  62. }
  63. }
  64. void loginAction() {
  65. string errorMessage = "";
  66. if (Username == "") {
  67. errorMessage = "Username is required";
  68. }
  69. if (Password == "") {
  70. if (errorMessage != "") {
  71. errorMessage += "\n";
  72. }
  73. errorMessage += "Password is required";
  74. }
  75. if (errorMessage == "") {
  76. errorColor.a = 0;
  77. StartCoroutine(loginCall());
  78. } else {
  79. errorText.text = errorMessage;
  80. errorColor.a = 1;
  81. }
  82. errorText.color = errorColor;
  83. }
  84. IEnumerator loginCall() {
  85. string postUrl = loginUrl + "name=" + UnityWebRequest.EscapeURL(Username);
  86. UnityWebRequest www = UnityWebRequest.Get(postUrl);
  87. yield return www.SendWebRequest();
  88. if (www.error != null) {
  89. errorText.text = "There was an error logging in " + www.error;
  90. }
  91. string result = www.downloadHandler.text;
  92. User u = new User();
  93. JsonUtility.FromJsonOverwrite(result, u);
  94. if (!u.userId.Equals("")) {
  95. byte[] pwd = Encoding.UTF8.GetBytes(u.salt + Password);
  96. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  97. string pass = Convert.ToBase64String(sha1.ComputeHash(pwd));
  98. if (pass.Equals(u.pass)) {
  99. errorColor.a = 0;
  100. errorText.color = errorColor;
  101. Int32.TryParse(u.userId, out int userId);
  102. PlayerPrefs.SetInt("UserId", userId);
  103. // load next scene
  104. SceneManager.LoadScene("MainMenu");
  105. } else {
  106. errorText.text = "Felaktig användare/lösenord";
  107. errorColor.a = 1;
  108. errorText.color = errorColor;
  109. }
  110. } else {
  111. errorText.text = "Användaren hittades inte";
  112. errorColor.a = 1;
  113. errorText.color = errorColor;
  114. }
  115. }
  116. }