Skip to content Skip to sidebar Skip to footer

Firebase Auth: Get Users By Email Or Phone Number

I am building an Android App where I am using different ways for the user to register himself, like Email/Password, Phone, Google, Facebook, Twitter. I also want the users to be ab

Solution 1:

You can get user's email or phone number like this:

FirebaseUseruser= FirebaseAuth.getInstance().getCurrentUser();
StringphoneNumber= user.getPhoneNumber();
Stringemail= user.getEmail();

Solution 2:

You can do this:

FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();

The above will get the currentuser that logged in.

then you can set his userid in the database, doing this:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid());
ref.child("phonenumber").setValue(number);

You would have this database:

Usersuseridname:name_herephonenumber:number_hereaddress:address_here

For the contacts simply do this:

DatabaseReference dbRef;
DatabaseReference cont=FirebaseDatabase.getInstance().getReference().child("Contact");
dbRef= cont.child(user.getUid());
dbRef=cont.child("name").setValue(name);

So in the Contact node you will have the userid and inside of it the names of the contacts that this user has added to the list.

Contact
    userid
       nameofContact1: name_here

Solution 3:

I presume you are using firebase for your authentication, since you tagged firebase in your question. Firebase authentication usually fetches the user's name during authentication so you can get those details during sign up, add them to your database and assign a unique id to each user. You can then create a friends node for each user on the system where their friends' ids will be stored. An example of sign up /sign in is shown below:

FirebaseAuthmAuth= FirebaseAuth.getInstance();
mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, newOnCompleteListener<AuthResult>() {
                    @OverridepublicvoidonComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, get available informationfinalFirebaseUseruser= mAuth.getCurrentUser();
                            if (user != null) {
                                if (user.getEmail() != null) {
                                    //since various methods are available, you may want to use namesStringname= user.getDisplayName();
                                    //if email exists, you can try this
                                    user.getEmail();
                                    //otherwise, this is sure to exist but you may want to tell your users //to complete their profiles later
                                    user.getUid()
                                }
                            } else {
                                //Notify user
                            }
                        } else {
                            //The whole process failed

                        }

                    }
                });

Solution 4:

You can use Firebase UID. Simply callFirebaseAuth.getInstance().getUid();

Solution 5:

If someone looking for how to get Auths Phone number, this is how to do so (Null Safe)

var user = FirebaseAuth.instance.currentUser;
String pn = user!.phoneNumber.toString();
print("user_phone_number " + pn);

Post a Comment for "Firebase Auth: Get Users By Email Or Phone Number"