Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

1. The Problem

I'm trying to implement simple Facebook and Google Sign-in buttons with Firebase and Flutter, however the Google version is acting very weird. When I print the FirebaseAuth.instance.currentUser() I either get errors or prior Facebook login data.

When I login with Facebook, my account does appear on Firebase, however, the Google login seems to do nothing.

Is this something I messed up in the code below or some incompatibility problem with AndroidX with parts of these libraries? Something else?

Also, it is not very clear to me if I have to put the Project public-facing name somewhere inside my project to make the integration with Firebase work (I had to do something similar to setup the Facebook Login button).

2. The Facebook Login

I had to replace logInWithReadPermissions with signInWithCredential because recent versions have changed their API. I've also tried to use previous versions of the packages, but encountered many errors (probably due to AndroidX):

final _auth = FirebaseAuth.instance;

Future<FirebaseUser> _loginWithFacebook () async {
  final facebookLogin = FacebookLogin();
  final result = await facebookLogin.logInWithReadPermissions(['email']);

  if (result.status == FacebookLoginStatus.loggedIn){
    final FacebookAccessToken accessToken = result.accessToken;
    AuthCredential credential = FacebookAuthProvider.getCredential(
      accessToken: accessToken.token,
    );

    AuthResult signInResult = await _auth.signInWithCredential(credential);

    FirebaseUser fbUser = signInResult.user;
    return fbUser;
  }
  else{
    return null;
  }
}

3. The Google Login

Again, signInWithCredential seems to be the more recent API:

Future<FirebaseUser> _loginWithGoogle () async{
  final GoogleSignIn _googleSignIn = GoogleSignIn();

  GoogleSignInAccount googleUser = await _googleSignIn.signIn();

  final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
  final AuthCredential credential = GoogleAuthProvider.getCredential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
  AuthResult signInResult = await _auth.signInWithCredential(credential);

  final FirebaseUser user = signInResult.user;

  print(user);

  return user;
}

Edit

I've tried it on an Android 9.0 (Pie) emulator and it still doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
276 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...