using System; using System.Collections; using System.Security.Cryptography; using System.Text; using Firebase; using Firebase.Auth; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Networking; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Register : MonoBehaviour { public GameObject passwordConfirm; public GameObject password; public GameObject email; public Text errorText; public Button registerButton; private string PasswordConfirm; private string Password; private string Email; private readonly string registerUserUrl = "http://nordh.xyz/narKampen/dbFiles/Register.php?"; private Color errorColor; private EventSystem system; private FirebaseAuth auth; private FirebaseUser user; // Start is called before the first frame update void Awake() { registerButton.onClick.AddListener(() => StartCoroutine(RegisterUser(Email, Password, PasswordConfirm))); system = EventSystem.current; errorColor = errorText.color; FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => { DependencyStatus dependencyStatus = task.Result; if (dependencyStatus.Equals(DependencyStatus.Available)) { InitializeFirebase(); } else { Debug.Log("Could not resolve all Firebase dependencis: " + dependencyStatus); } }); } private void InitializeFirebase() { auth = FirebaseAuth.DefaultInstance; } // Update is called once per frame void Update() { Password = password.GetComponent().text; PasswordConfirm = passwordConfirm.GetComponent().text; Email = email.GetComponent().text; if (Input.GetKeyDown(KeyCode.Tab)) { Selectable next = system.currentSelectedGameObject.GetComponent().FindSelectableOnDown(); if (next != null) { InputField inputfield = next.GetComponent(); if (inputfield != null) { inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret } system.SetSelectedGameObject(next.gameObject, new BaseEventData(system)); } } else if ((Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift)) && Input.GetKeyDown(KeyCode.Tab)) { Selectable next = system.currentSelectedGameObject.GetComponent().FindSelectableOnUp(); if (next != null) { InputField inputfield = next.GetComponent(); if (inputfield != null) { inputfield.OnPointerClick(new PointerEventData(system)); //if it's an input field, also set the text caret } system.SetSelectedGameObject(next.gameObject, new BaseEventData(system)); } } } private IEnumerator RegisterUser(string email, string password, string passwordConfirm) { Debug.Log("Starting registerUser"); if (email == "" || email.Equals("Email")) { errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_EMAIL_EMPTY"); } else if (password != passwordConfirm) { errorText.text = LocalizationManager.Instance.GetText("REGISTER_ERROR_PASSWORD_DONT_MATCH"); } else { Debug.Log("Trying to register user " + Email + " password " + Password); var RegisterTask = auth.CreateUserWithEmailAndPasswordAsync(email, password); yield return new WaitUntil(predicate: () => RegisterTask.IsCompleted); if (RegisterTask.Exception != null) { Debug.Log("EXCEPTION at regiser " + RegisterTask.Exception); FirebaseException firebaseEx = RegisterTask.Exception.GetBaseException() as FirebaseException; AuthError errorCode = (AuthError)firebaseEx.ErrorCode; // TODO Localize message string message = "Register Failed!"; switch (errorCode) { case AuthError.MissingEmail: message = "Missing Email"; break; case AuthError.MissingPassword: message = "Missing Password"; break; case AuthError.WeakPassword: message = "Weak Password"; break; case AuthError.EmailAlreadyInUse: message = "Email already in use"; break; } errorText.text = message; } else { user = RegisterTask.Result; if (user != null) { UserProfile profile = new UserProfile { DisplayName = email }; var ProfileTask = user.UpdateUserProfileAsync(profile); yield return new WaitUntil(predicate: () => ProfileTask.IsCompleted); if (ProfileTask.Exception != null) { FirebaseException firebaseEx = ProfileTask.Exception.GetBaseException() as FirebaseException; AuthError errorCode = (AuthError)firebaseEx.ErrorCode; errorText.text = "Email set Failed!"; } else { // DatabaseController.Instance.InsertUser(registerUsernameTextField.text, user.UserId); errorText.text = ""; SceneManager.LoadScene("MainMenu"); } } } } if (errorText.text.Length > 0) { errorColor.a = 1; errorText.color = errorColor; } else { errorColor.a = 0; errorText.color = errorColor; } } }