How to connect facebook, firebase and flutter?

5,939

In flutter you need use flutter_facebook_login plugin take a look here to see how to get the plugin and setup your flutter app to make use of this plugin. You can also check this article that is step-by-step about how setup you project and contains code example too but the API used is out of date.

Here a snippet with updated API showing how to achieve login in firebase with facebook account.

/// This mehtod makes the real auth
Future<FirebaseUser> firebaseAuthWithFacebook({@required FacebookAccessToken token}) async {

    AuthCredential credential= FacebookAuthProvider.getCredential(accessToken: token.token);
    FirebaseUser firebaseUser = await _authInstance.signInWithCredential(credential);
    return firebaseUser;
}

In your code you're using _auth.signInWithFacebook method that is deprecated and you should replaced by signInWithCredential updating you firebase_auth plugin version.

///This object comes from facebook_login_plugin package
final facebookLogin = new FacebookLogin();

final facebookLoginResult = await facebookLogin
        .logInWithReadPermissions(['email', 'public_profile']);

    switch (facebookLoginResult.status) {
      case FacebookLoginStatus.error:
        print("Error");
        break;

      case FacebookLoginStatus.cancelledByUser:
        print("CancelledByUser");
        break;

      case FacebookLoginStatus.loggedIn:
        print("LoggedIn");
        /// calling the auth mehtod and getting the logged user
        var firebaseUser = await firebaseAuthWithFacebook(
            token: facebookLoginResult.accessToken);
     }
}
Share:
5,939
Mike1982
Author by

Mike1982

I'm a first year computer information systems student at Chico State. I'm 34 years old. If I'm not sleeping or eating, I'm either doing homework or practicing programming. I'm familiar with C++, Java, Javascript, HTML, CSS, and SQL. I'm not really good at any of them, but that's what practice is for. I would like to become proficient in all of them, but if I had to choose one that I could start a career in, it would be Java. Anyways, that's about all there is to my boring life, lol.

Updated on December 10, 2022

Comments

  • Mike1982
    Mike1982 over 1 year

    I'm following the instructions for incorporating facebook with android projects found here https://developers.facebook.com/apps/318154048893918/fb-login/quickstart/ and there is a step to download the Facebook SDK, but after that, it doesn't tell me where to put the file. The import statement it tells me to add won't work (says target of uri doesn't exist).

    I'm trying to add the facebook user to our firebase database when they log in. I'm using flutter in android studio.

    There doesn't seem to be anything of use in the console log, except that print statement doesn't print anything. Any ideas?

    Here's my code to log in the user.

    import com.facebook.FacebookSdk;
    import com.facebook.appevents.AppEventsLogger;
    
    Future<FirebaseUser> initiateFacebookLogin() async {
    
    final FacebookLoginResult result =
    await facebookLogin.logInWithReadPermissions(['email', 'public_profile']);
    
    FirebaseUser user =
    await _auth.signInWithFacebook(accessToken: result.accessToken.token);
    //Token: ${accessToken.token}
    
    ProviderDetails userInfo = new ProviderDetails(
        user.providerId, user.uid, user.displayName, user.photoUrl, user.email);
    
    List<ProviderDetails> providerData = new List<ProviderDetails>();
    providerData.add(userInfo);
    print(user.displayName);
    addToDatabase(user.uid, user.displayName, user.displayName, user.email);
    return user;
    
    }
    
  • Marcos Boaventura
    Marcos Boaventura about 5 years
    You can also check this
  • Mike1982
    Mike1982 about 5 years
    ah ha ok. Yeah I have the facebook plugin. So looks like I need to do signInWithCredential, not signInWithFacebook. Thanks!