Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. using Firebase.Auth;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Firebase;
  7. using Firebase.Unity.Editor;
  8. using Firebase.Database;
  9. using System.Threading.Tasks;
  10.  
  11. public class User
  12. {
  13. public string uid;
  14. public int searching = 0;
  15.  
  16. public User()
  17. {
  18. }
  19.  
  20. public User(string uid, int searching)
  21. {
  22. this.uid = uid;
  23. this.searching = searching;
  24. }
  25.  
  26. public Dictionary<string, object> ToDictionary()
  27. {
  28. Dictionary<string, object> result = new Dictionary<string, object>();
  29. result["uid"] = uid;
  30. result["points"] = searching;
  31.  
  32. return result;
  33. }
  34. }
  35.  
  36. public class Registration : MonoBehaviour {
  37.  
  38. private FirebaseAuth auth;
  39.  
  40. void Start ()
  41. {
  42. auth = FirebaseAuth.DefaultInstance;
  43. }
  44.  
  45. public void HandleRegistering()
  46. {
  47. string email = transform.parent.GetChild(2).gameObject.GetComponent<InputField>().text;
  48. string password = transform.parent.GetChild(3).gameObject.GetComponent<InputField>().text;
  49. string repeatedPassword = transform.parent.GetChild(4).gameObject.GetComponent<InputField>().text;
  50. Register(email, password, repeatedPassword);
  51. }
  52.  
  53. private void Register(string email, string password, string repeatedPassword)
  54. {
  55. if (!string.Equals(password, repeatedPassword))
  56. {
  57. Debug.Log("Niezgodnosc hasla i powtorzonego hasla");
  58. return;
  59. }
  60. if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
  61. {
  62. //Error handling
  63. return;
  64. }
  65.  
  66. auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(task =>
  67. {
  68. if (task.IsCanceled)
  69. {
  70. Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
  71. return;
  72. }
  73. if (task.IsFaulted)
  74. {
  75. Debug.LogError("CreateUserWithEmailAndPasswordAsync error: " + task.Exception);
  76. if (task.Exception.InnerExceptions.Count > 0)
  77. UpdateErrorMessage(task.Exception.InnerExceptions[0].Message);
  78. return;
  79. }
  80.  
  81. FirebaseUser newUser = task.Result; // Firebase user has been created.
  82. Debug.LogFormat("Firebase user created successfully: {0} ({1})",
  83. newUser.DisplayName, newUser.UserId);
  84. UpdateErrorMessage("Signup Success");
  85.  
  86. PushUser();
  87. StateManager.SetState(StateManager.MenuState.MAIN_MENU);
  88.  
  89. });
  90. }
  91.  
  92. public void PushUser()
  93. {
  94. FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://dice-x-game.firebaseio.com/");
  95. DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
  96. string key = reference.Child("users").Push().Key;
  97.  
  98. Dictionary<string, object> childUpdates = new Dictionary<string, object>();
  99.  
  100. User u = new User(Firebase.Auth.FirebaseAuth.DefaultInstance.CurrentUser.UserId, 10);
  101. childUpdates["/users/" + Firebase.Auth.FirebaseAuth.DefaultInstance.CurrentUser.UserId] = u.ToDictionary();
  102.  
  103. reference.UpdateChildrenAsync(childUpdates);
  104.  
  105. }
  106.  
  107. private void UpdateErrorMessage(string message)
  108. {
  109. Debug.Log("Update error");
  110. }
  111.  
  112. private void ClearErrorMessage()
  113. {
  114. Debug.Log("Update error");
  115. }
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement