How to get Facebook Email from flutter_facebook_login in Firebase?

1.7k views Asked by At

I am using flutter_facebook_login plugin(3.0.0). But my Facebook email doesn't appear inside my Firebase user identifier column; instead, I see this "--". Please help!

Future loginWithFacebook() async {
    FacebookLogin facebookLogin = FacebookLogin();
    final result = await facebookLogin.logIn(['email', 'public_profile']);
    //result.accessToken.
    final token = result.accessToken.token;

    print('Facebook token userID : ${result.accessToken.permissions}');
    final graphResponse = await http.get(
        'https://graph.facebook.com/v2.12/me?fields=name,first_name,last_name,email&access_token=${token}');

    final profile = jsonDecode(graphResponse.body);
    print(profile);

    if (result.status == FacebookLoginStatus.loggedIn) {
      final credential = FacebookAuthProvider.getCredential(accessToken: token);
      
      FirebaseUser fbUser = (await _auth.signInWithCredential(credential)).user;

      //print('Our credential is : $credential');
      print('Facebook firebase user ${fbUser.}');
    }

    return _userFromFacebookLogin(profile);
  }
}
1

There are 1 answers

0
Gursewak Singh On

Possible Points.

  1. Some Facebook accounts are created using Mobile numbers, so whenever we request for email address we get an empty string.

  2. Email was set to "--" on firebase auth due to missing permission to read email, which fixed by:

    final FacebookLoginResult facebookLoginResult = await facebook_Login.logIn(['email', 'public_profile']);
    
  3. After reading post in firebase-talk google group here https://groups.google.com/forum/#!topic/firebase-talk/gPGNq-IkTLo, I found out the answer. The issue was happened because I'm using "Allow creation of multiple accounts with the same email address" in Firebase Auth sign-in method.

    So I change the option into: "Prevent creation of multiple accounts with the same email address" can it's working properly now. It's simple as that. It's true I need more logic to merge accounts having the same email address, but it's okay.

    Maybe everyone else having the same issue, can also try this, and hopefully it's solved as well.