| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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<InputField>().text;
- PasswordConfirm = passwordConfirm.GetComponent<InputField>().text;
- Email = email.GetComponent<InputField>().text;
- if (Input.GetKeyDown(KeyCode.Tab))
- {
- Selectable next = system.currentSelectedGameObject.GetComponent<Selectable>().FindSelectableOnDown();
- if (next != null)
- {
- InputField inputfield = next.GetComponent<InputField>();
- 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<Selectable>().FindSelectableOnUp();
- if (next != null)
- {
- InputField inputfield = next.GetComponent<InputField>();
- 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;
- }
- }
- }
|