1
votes

I'm trying to get the email from the user that's currently authenticated using the Facebook Firebase provider. The email is listed under the Identifier field inside the Project's Firebase Authentication Console: enter image description here

However when I invoke firebase.auth().currentUser the user information loads, however the email field is null. Any ideas on how to get the Identifier (which is where I see the email address) from Firebase? Is this even possible?

Below is the code I'm using:

    componentDidMount() {

      let user = firebase.auth().currentUser;
      let name, email, photoUrl, uid, emailVerified;

      if (user !== null) {
       name = user.displayName;
       email = user.email;
       photoUrl = user.photoURL;
       emailVerified = user.emailVerified;
       uid = user.uid;

       console.log(name, email, photoUrl, emailVerified, uid);
      }

    }

Note: Prevent creation of multiple accounts with the same email address is enabled in Firebase. Also, Facebook API permissions are set to ['public_profile', 'email']

2
why not use console.log directly with "user" instead of listing those fields separately? - andyrandy
So when you call console.log, everything else is logged, just not the email? - Jen Person
Thanks for the tips. I inspected user and found a providerData property that contained the info I was looking for (including the email). See my answer below. - Juan Marco

2 Answers

1
votes

After some testing and debugging I found that the email field will be populated if you're using a regular Firebase Email/Password Sign In method. However, if you're using another Sign In provider method such as Facebook, the email field will appear null (not sure why).

Further inspection of the user object revealed a providerData property. It's an array that contains all the provider information (including the email address):

enter image description here

So, I updated my code to accommodate this:

    componentDidMount() {

      let user = firebase.auth().currentUser;

      let name, email, photoUrl, uid, emailVerified;

      if (user) {
        name = user.displayName; 
        email = user.email;
        photoUrl = user.photoURL; 
        emailVerified = user.emailVerified;
        uid = user.uid;

        if (!email) {
          email = user.providerData[0].email;
        }

        console.log(name, email, photoUrl, emailVerified, uid);
      }

    }
0
votes

In my case, the getEmail() method always returns data for three sign-in possibilities (if user gave authorization to my app to show/use email): Sign in with Email, Sign in with Google, Sign in with Facebook.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Log.v(TAG,"user.getEmail():"+user.getEmail());

if (user.getEmail() == null){
    // User did not authorize the app to show/user email
}
else {
   Log.v(TAG,"user.getEmail():"+user.getEmail());   
}