Search Unity

Put in Firebase Database after create a new user

Discussion in 'Editor & General Support' started by RAZ-077, Oct 22, 2019.

  1. RAZ-077

    RAZ-077

    Joined:
    Sep 8, 2019
    Posts:
    1
    Hi,

    I'm using Firebase authentication to manage my users.
    After creating a new user, I want to put this user in a Firebase Realtime Database using API Rest.
    My problem is I can do that only using a couroutine (I'm sure isn't the good way :))
    Could you tell me how do that without couroutine ?

    Thanks

    Rémy


    Code (CSharp):
    1. public void CreateNewAccount()
    2.     {
    3.         if (IsControlsOK())
    4.         {
    5.             string email = newPlayerMail.text;
    6.             string password = newPassword1.text;
    7.             auth.CreateUserWithEmailAndPasswordAsync(email, password).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.  
    20.                 // Firebase user has been created.
    21.                 FirebaseUser newUser = task.Result;
    22.                 Debug.LogFormat("Firebase user created successfully: {0} ({1})",
    23.                     newUser.DisplayName, newUser.UserId);
    24.  
    25.                 //I would like to put the PostNameToDataBase() method here but doesn't work.
    26.  
    27.             });
    28.             StartCoroutine(TempoCreateNewAccount());
    29.         }
    30.     }


    Code (CSharp):
    1. IEnumerator TempoCreateNewAccount()
    2.     {
    3.         yield return new WaitForSeconds(2f);
    4.         PostNameToDataBase();
    5.     }


    Code (CSharp):
    1. public void PostNameToDataBase()
    2.     {
    3.         User newPseudo = new User(newPlayerName.text, auth.CurrentUser.Email, auth.CurrentUser.UserId, 0);
    4.         RestClient.Put("https://xxxxxxxxxxxxxxxxxxxx/" + auth.CurrentUser.UserId + ".json", newPseudo).Then(response =>
    5.         {
    6.             RetrievePseudoFromDataBase();
    7.         });
    8.     }
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Instead of calling StartCoroutine you would likely just call PostNameToDataBase() directly instead