Register.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Collections;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using Firebase;
  6. using Firebase.Auth;
  7. using UnityEngine;
  8. using UnityEngine.EventSystems;
  9. using UnityEngine.Networking;
  10. using UnityEngine.SceneManagement;
  11. using UnityEngine.UI;
  12. public class Register : MonoBehaviour
  13. {
  14. public GameObject passwordConfirm;
  15. public GameObject password;
  16. public GameObject email;
  17. public Text errorText;
  18. public Button registerButton;
  19. private string PasswordConfirm;
  20. private string Password;
  21. private string Email;
  22. private readonly string registerUserUrl = "http://nordh.xyz/narKampen/dbFiles/Register.php?";
  23. private Color errorColor;
  24. private EventSystem system;
  25. private FirebaseAuth auth;
  26. private FirebaseUser user;
  27. // Start is called before the first frame update
  28. void Awake()
  29. {
  30. registerButton.onClick.AddListener(() => StartCoroutine(RegisterUser(Email, Password, PasswordConfirm)));
  31. system = EventSystem.current;
  32. errorColor = errorText.color;
  33. FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
  34. {
  35. DependencyStatus dependencyStatus = task.Result;
  36. if (dependencyStatus.Equals(DependencyStatus.Available))
  37. {
  38. InitializeFirebase();
  39. }
  40. else
  41. {
  42. Debug.Log("Could not resolve all Firebase dependencis: " + dependencyStatus);
  43. }
  44. });
  45. }
  46. private void InitializeFirebase()
  47. {
  48. auth = FirebaseAuth.DefaultInstance;
  49. }
  50. // Update is called once per frame
  51. void Update()
  52. {
  53. Password = password.GetComponent<InputField>().text;
  54. PasswordConfirm = passwordConfirm.GetComponent<InputField>().text;
  55. Email = email.GetComponent<InputField>().text;
  56. if (Input.GetKeyDown(KeyCode.Tab))
  57. {
  58. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
  59. if (next != null)
  60. {
  61. InputField inputfield = next.GetComponent<InputField>();
  62. if (inputfield != null)
  63. {
  64. inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  65. }
  66. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  67. }
  68. }
  69. else if ((Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Tab))
  70. {
  71. Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnUp();
  72. if (next != null)
  73. {
  74. InputField inputfield = next.GetComponent<InputField>();
  75. if (inputfield != null)
  76. {
  77. inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret
  78. }
  79. system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
  80. }
  81. }
  82. }
  83. private IEnumerator RegisterUser(string email, string password, string passwordConfirm)
  84. {
  85. Debug.Log("Starting registerUser");
  86. if (email == "" || email.Equals("Email"))
  87. {
  88. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_EMAIL_EMPTY");
  89. }
  90. else if (password != passwordConfirm)
  91. {
  92. errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_DONT_MATCH");
  93. }
  94. else
  95. {
  96. Debug.Log("Trying to register user " + Email + " password " + Password);
  97. var RegisterTask = auth.CreateUserWithEmailAndPasswordAsync(email, password);
  98. yield return new WaitUntil(predicate: () => RegisterTask.IsCompleted);
  99. if (RegisterTask.Exception != null)
  100. {
  101. Debug.Log("EXCEPTION at regiser " + RegisterTask.Exception);
  102. FirebaseException firebaseEx = RegisterTask.Exception.GetBaseException() as FirebaseException;
  103. AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
  104. // TODO Localize message
  105. string message = "Register Failed!";
  106. switch (errorCode)
  107. {
  108. case AuthError.MissingEmail:
  109. message = "Missing Email";
  110. break;
  111. case AuthError.MissingPassword:
  112. message = "Missing Password";
  113. break;
  114. case AuthError.WeakPassword:
  115. message = "Weak Password";
  116. break;
  117. case AuthError.EmailAlreadyInUse:
  118. message = "Email already in use";
  119. break;
  120. }
  121. errorText.text = message;
  122. }
  123. else
  124. {
  125. user = RegisterTask.Result;
  126. if (user != null)
  127. {
  128. UserProfile profile = new UserProfile { DisplayName = email };
  129. var ProfileTask = user.UpdateUserProfileAsync(profile);
  130. yield return new WaitUntil(predicate: () => ProfileTask.IsCompleted);
  131. if (ProfileTask.Exception != null)
  132. {
  133. FirebaseException firebaseEx = ProfileTask.Exception.GetBaseException() as FirebaseException;
  134. AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
  135. errorText.text = "Email set Failed!";
  136. }
  137. else
  138. {
  139. // DatabaseController.Instance.InsertUser(registerUsernameTextField.text, user.UserId);
  140. errorText.text = "";
  141. SceneManager.LoadScene("MainMenu");
  142. }
  143. }
  144. }
  145. }
  146. if (errorText.text.Length > 0)
  147. {
  148. errorColor.a = 1;
  149. errorText.color = errorColor;
  150. }
  151. else
  152. {
  153. errorColor.a = 0;
  154. errorText.color = errorColor;
  155. }
  156. }
  157. }