Search Unity

Questions about Firebase

Discussion in 'Formats & External Tools' started by matthewbrand, Feb 19, 2017.

  1. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25
    I am just starting to check out Firebase. To start, I want to use it for Authentication and Database. I used their guide to set up my project, so I have a project in the backend, I have the SDK for Authentication imported, I have the plist file in my Unity project.

    I've tried a few things, all within the Unity editor, and they don't seem to be working, at least on the backend side. In Unity I am hitting all the right spots in my code, according to my debug messages. I am creating a Firebase user, but the user doesn't appear in the Firebase backend. The code indicates that the user is created though. Here is the code I'm using:

    Code (CSharp):
    1. FirebaseAuth _auth;
    2. FirebaseUser _user;
    3.  
    4. void Start()
    5. {
    6.     InitializeFirebase();
    7.     _auth.CreateUserWithEmailAndPasswordAsync("mattbrand@gmail.com", "testtest").ContinueWith(task =>
    8.     {
    9.         if (task.IsCanceled)
    10.         {
    11.             Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
    12.             return;
    13.         }
    14.         if (task.IsFaulted)
    15.         {
    16.             Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    17.             return;
    18.         }
    19.         Debug.Log("Complete");
    20.  
    21.         // Firebase user has been created.
    22.         _user = task.Result;
    23.         Debug.LogFormat("Firebase user created successfully: " + _user.DisplayName + " " + _user.UserId);
    24.     });
    25. }
    26.  
    27. void OnDestroy()
    28. {
    29.     if (_auth != null)
    30.     {
    31.         _auth.StateChanged -= AuthStateChanged;
    32.         _auth = null;
    33.     }
    34. }
    35.  
    36. void InitializeFirebase()
    37. {
    38.     Debug.Log("InitializeFirebase");
    39.     _auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    40.     _auth.StateChanged += AuthStateChanged;
    41.     AuthStateChanged(this, null);
    42. }
    43.  
    44. void AuthStateChanged(object sender, System.EventArgs eventArgs)
    45. {
    46.     Debug.Log("AuthStateChanged!");
    47.     if (_auth != null)
    48.         Debug.Log("_auth.CurrentUser = " + _auth.CurrentUser + " _user = " + _user);
    49.     if (_auth.CurrentUser != _user)
    50.     {
    51.         bool signedIn = _user != _auth.CurrentUser && _auth.CurrentUser != null;
    52.         if (!signedIn && _user != null)
    53.         {
    54.             Debug.Log("Signed out " + _user.UserId);
    55.         }
    56.         _user = _auth.CurrentUser;
    57.         if (signedIn)
    58.         {
    59.             Debug.Log("Signed in " + _user.UserId);
    60.         }
    61.     }
    62. }
    Anyone with experience with Firebase can help me on this?
     
  2. matthewbrand

    matthewbrand

    Joined:
    Jan 9, 2017
    Posts:
    25