| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Security.Cryptography;
- using System.Text;
- using Firebase;
- using Firebase.Auth;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.Networking;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class Login : MonoBehaviour
- {
- public InputField usernameInputField;
- public InputField passwordInputField;
- //[SerializeField] Button swedishLangButton;
- // [SerializeField] Button englishLangButton;
- public Button loginButton;
- public Toggle keepSignedIn;
- public Text errorText;
- private string Username;
- private string Password;
- private readonly string loginUrl = "http://nordh.xyz/narKampen/dbFiles/Login.php?";
- private Color errorColor;
- private EventSystem system;
- private FirebaseAuth auth;
- private FirebaseUser user;
- [SerializeField] GameObject firebaseController;
- [Serializable]
- public class User
- {
- public string userId;
- public string pass;
- public string salt;
- }
- private void Awake()
- {
- if (Database.Instance.IsKeepSignedIn())
- {
- SceneManager.LoadScene("MainMenu");
- }
- else
- {
- FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
- {
- DependencyStatus dependencyStatus = task.Result;
- if (dependencyStatus.Equals(DependencyStatus.Available))
- {
- InitializeFirebase();
- }
- else
- {
- Debug.Log("Could not resolve all Firebase dependencis: " + dependencyStatus);
- }
- });
- // loginButton.onClick.AddListener(LoginAction);
- loginButton.onClick.AddListener(() => StartCoroutine(NewLoginAction(usernameInputField.text, passwordInputField.text)));
- errorColor = errorText.color;
- system = EventSystem.current;
- usernameInputField.Select();
- usernameInputField.gameObject.SetActive(true);
- }
- }
- private void InitializeFirebase()
- {
- Debug.Log("Init firebase");
- auth = FirebaseAuth.DefaultInstance;
- }
- private void SwitchLanguage(int langId)
- {
- LocalizationManager.Instance.currentLanguageID = langId;
- TextLocalization[] texts = FindObjectsOfType(typeof(TextLocalization)) as TextLocalization[];
- foreach (TextLocalization tl in texts)
- {
- tl.UpdateText();
- }
- InputFieldLocalization[] inputFields = FindObjectsOfType(typeof(InputFieldLocalization)) as InputFieldLocalization[];
- foreach (InputFieldLocalization ifl in inputFields)
- {
- ifl.UpdateText();
- }
- ButtonLocalization[] buttonLocale = FindObjectsOfType(typeof(ButtonLocalization)) as ButtonLocalization[];
- foreach (ButtonLocalization bl in buttonLocale)
- {
- bl.UpdateText();
- }
- }
- private void Update()
- {
- Username = usernameInputField.text;
- Password = passwordInputField.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));
- system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
- }
- }
- }
- private IEnumerator NewLoginAction(string username, string password)
- {
- var LoginTask = auth.SignInWithEmailAndPasswordAsync(username, password);
- yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);
- if (LoginTask.Exception != null)
- {
- errorColor.a = 1;
- // There is an error, handle it
- Debug.LogWarning(message: $"Failed to register task with {LoginTask.Exception}");
- FirebaseException firebaseEx = LoginTask.Exception.GetBaseException() as FirebaseException;
- AuthError errorCode = (AuthError)firebaseEx.ErrorCode;
- string message = "Login failed!";
- switch (errorCode)
- {
- case AuthError.MissingEmail:
- message = "Missing email";
- break;
- case AuthError.MissingPassword:
- message = "Missing password";
- break;
- case AuthError.WrongPassword:
- message = "Wrong password";
- break;
- case AuthError.InvalidEmail:
- message = "Invalid email";
- break;
- case AuthError.UserNotFound:
- message = "Account does not exist";
- break;
- }
- errorText.text = message;
- errorText.color = errorColor;
- }
- else
- {
- errorText.text = "";
- errorColor.a = 0;
- errorText.color = errorColor;
- user = LoginTask.Result;
- SceneManager.LoadScene("MainMenu");
- }
- }
- }
|