Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Firebase.Auth;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Firebase;
- using Firebase.Unity.Editor;
- using Firebase.Database;
- using System.Threading.Tasks;
- public class User
- {
- public string uid;
- public int searching = 0;
- public User()
- {
- }
- public User(string uid, int searching)
- {
- this.uid = uid;
- this.searching = searching;
- }
- public Dictionary<string, object> ToDictionary()
- {
- Dictionary<string, object> result = new Dictionary<string, object>();
- result["uid"] = uid;
- result["points"] = searching;
- return result;
- }
- }
- public class Registration : MonoBehaviour {
- private FirebaseAuth auth;
- void Start ()
- {
- auth = FirebaseAuth.DefaultInstance;
- }
- public void HandleRegistering()
- {
- string email = transform.parent.GetChild(2).gameObject.GetComponent<InputField>().text;
- string password = transform.parent.GetChild(3).gameObject.GetComponent<InputField>().text;
- string repeatedPassword = transform.parent.GetChild(4).gameObject.GetComponent<InputField>().text;
- Register(email, password, repeatedPassword);
- }
- private void Register(string email, string password, string repeatedPassword)
- {
- if (!string.Equals(password, repeatedPassword))
- {
- Debug.Log("Niezgodnosc hasla i powtorzonego hasla");
- return;
- }
- if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
- {
- //Error handling
- return;
- }
- auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
- {
- if (task.IsCanceled)
- {
- Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
- return;
- }
- if (task.IsFaulted)
- {
- Debug.LogError("CreateUserWithEmailAndPasswordAsync error: " + task.Exception);
- if (task.Exception.InnerExceptions.Count > 0)
- UpdateErrorMessage(task.Exception.InnerExceptions[0].Message);
- return;
- }
- FirebaseUser newUser = task.Result; // Firebase user has been created.
- Debug.LogFormat("Firebase user created successfully: {0} ({1})",
- newUser.DisplayName, newUser.UserId);
- UpdateErrorMessage("Signup Success");
- PushUser();
- StateManager.SetState(StateManager.MenuState.MAIN_MENU);
- });
- }
- public void PushUser()
- {
- FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://dice-x-game.firebaseio.com/");
- DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
- string key = reference.Child("users").Push().Key;
- Dictionary<string, object> childUpdates = new Dictionary<string, object>();
- User u = new User(Firebase.Auth.FirebaseAuth.DefaultInstance.CurrentUser.UserId, 10);
- childUpdates["/users/" + Firebase.Auth.FirebaseAuth.DefaultInstance.CurrentUser.UserId] = u.ToDictionary();
- reference.UpdateChildrenAsync(childUpdates);
- }
- private void UpdateErrorMessage(string message)
- {
- Debug.Log("Update error");
- }
- private void ClearErrorMessage()
- {
- Debug.Log("Update error");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement