Login.cs 3.5 KB

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