Login.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using Firebase;
  7. using Firebase.Auth;
  8. using UnityEngine;
  9. using UnityEngine.Events;
  10. using UnityEngine.EventSystems;
  11. using UnityEngine.Networking;
  12. using UnityEngine.SceneManagement;
  13. using UnityEngine.UI;
  14. public class Login : MonoBehaviour
  15. {
  16. public InputField usernameInputField;
  17. public InputField passwordInputField;
  18. //[SerializeField] Button swedishLangButton;
  19. // [SerializeField] Button englishLangButton;
  20. public Button loginButton;
  21. public Toggle keepSignedIn;
  22. public Text errorText;
  23. private string Username;
  24. private string Password;
  25. private readonly string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
  26. private Color errorColor;
  27. private EventSystem system;
  28. private FirebaseAuth auth;
  29. private FirebaseUser user;
  30. [SerializeField] GameObject firebaseController;
  31. [Serializable]
  32. public class User
  33. {
  34. public string userId;
  35. public string pass;
  36. public string salt;
  37. }
  38. private void Awake()
  39. {
  40. if (Database.Instance.IsKeepSignedIn())
  41. {
  42. SceneManager.LoadScene("MainMenu");
  43. }
  44. else
  45. {
  46. FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
  47. {
  48. DependencyStatus dependencyStatus = task.Result;
  49. if (dependencyStatus.Equals(DependencyStatus.Available))
  50. {
  51. InitializeFirebase();
  52. }
  53. else
  54. {
  55. Debug.Log("Could not resolve all Firebase dependencis: " + dependencyStatus);
  56. }
  57. });
  58. // loginButton.onClick.AddListener(LoginAction);
  59. loginButton.onClick.AddListener(() => StartCoroutine(NewLoginAction(usernameInputField.text, passwordInputField.text)));
  60. errorColor = errorText.color;
  61. system = EventSystem.current;
  62. usernameInputField.Select();
  63. usernameInputField.gameObject.SetActive(true);
  64. }
  65. }
  66. private void InitializeFirebase()
  67. {
  68. Debug.Log("Init firebase");
  69. auth = FirebaseAuth.DefaultInstance;
  70. }
  71. private void SwitchLanguage(int langId)
  72. {
  73. LocalizationManager.Instance.currentLanguageID = langId;
  74. TextLocalization[] texts = FindObjectsOfType(typeof(TextLocalization)) as TextLocalization[];
  75. foreach (TextLocalization tl in texts)
  76. {
  77. tl.UpdateText();
  78. }
  79. InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
  80. foreach (InputFieldLocalization ifl in inputFields)
  81. {
  82. ifl.UpdateText();
  83. }
  84. ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
  85. foreach (ButtonLocalization bl in buttonLocale)
  86. {
  87. bl.UpdateText();
  88. }
  89. }
  90. private void Update()
  91. {
  92. Username = usernameInputField.text;
  93. Password = passwordInputField.text;
  94. if (Input.GetKeyDown(KeyCode.Tab))
  95. {
  96. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  97. if (next != null)
  98. {
  99. InputField inputfield = next.GetComponent<InputField>();
  100. if (inputfield != null) inputfield.OnPointerClick(new PointerEventData(system));
  101. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  102. }
  103. }
  104. }
  105. private IEnumerator NewLoginAction(string username, string password)
  106. {
  107. var LoginTask = auth.SignInWithEmailAndPasswordAsync(username, password);
  108. yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);
  109. if (LoginTask.Exception != null)
  110. {
  111. errorColor.a = 1;
  112. // There is an error, handle it
  113. Debug.LogWarning(message: $"Failed to register task with {LoginTask.Exception}");
  114. FirebaseException firebaseEx = LoginTask.Exception.GetBaseException() as FirebaseException;
  115. AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
  116. string message = "Login failed!";
  117. switch (errorCode)
  118. {
  119. case AuthError.MissingEmail:
  120. message = "Missing email";
  121. break;
  122. case AuthError.MissingPassword:
  123. message = "Missing password";
  124. break;
  125. case AuthError.WrongPassword:
  126. message = "Wrong password";
  127. break;
  128. case AuthError.InvalidEmail:
  129. message = "Invalid email";
  130. break;
  131. case AuthError.UserNotFound:
  132. message = "Account does not exist";
  133. break;
  134. }
  135. errorText.text = message;
  136. errorText.color = errorColor;
  137. }
  138. else
  139. {
  140. errorText.text = "";
  141. errorColor.a = 0;
  142. errorText.color = errorColor;
  143. user = LoginTask.Result;
  144. SceneManager.LoadScene("MainMenu");
  145. }
  146. }
  147. }