Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

Flutter Google Login not Getting Registered in Firebase

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

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...