2

I am using firebase Email/password authentication for both signup and sign in. However during the Sign up process I would like to get the user UID and use that to create a new database in firebase with other user information like Name and phonenumber. I know its easier to get the UID with a login process, however is there a way to do this with signup, assuming sign up has been successful. I am using android studio.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mpanga
  • 107
  • 3
  • 11
  • 1
    Did you try anything yet? Because this topic has been covered a few times already. For example [here](http://stackoverflow.com/questions/32151178/how-do-you-include-a-username-when-storing-email-and-password-using-firebase-ba/32151335#32151335) or probably quite a few of the [items in this list](http://stackoverflow.com/search?q=%5Bfirebase-authentication%5D+%5Bfirebase-database%5D%5Bandroid%5D+store+users). – Frank van Puffelen Aug 26 '16 at 15:11

1 Answers1

4

You can get the user information from the task returned by the completion listener:

    mAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                    if (task.isSuccessful()) {
                        FirebaseUser user = task.getResult().getUser();
                        Log.d(TAG, "onComplete: uid=" + user.getUid());
                    }
                }
            });
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I saw this in the documentation and I already tried using. however I am getting the error that firebase.auth not found. I had this error before and I fixed it so am wondering why its coming up again. – mpanga Aug 26 '16 at 15:09
  • @PaulMpanga: Can you post the error? The code runs successfully for me. – Bob Snyder Aug 26 '16 at 15:11
  • 1
    it worked, my User class was written weird. thanks!! – mpanga Aug 26 '16 at 15:32
  • will i be able to add the `userid` in database inside `task.isSuccessful`? Example inside the if , do this `newStudent.child("uid").setValue(mCurrentUser.getUid());`. The `newStudent` is a DatabaseReference will this work? – Peter Haddad Oct 18 '17 at 06:59
  • @PeterHaddad: Yes – Bob Snyder Oct 18 '17 at 14:19