简体   繁体   中英

Firebase Auth not working in Flutter

Somehow Firebase Authentication doesn't work in my Implementation. I implemented Google SignIn with Firebase Authentication following the Firebase Codelab , but changed the ensureLoggedIn function to:

Future<Null> _ensureLoggedIn() async {
  GoogleSignInAccount user = googleSignIn.currentUser;
  print('SIGNIN');
  if (user == null) user = await googleSignIn.signInSilently();
  if (user == null) {
    user = await googleSignIn.signIn();
    print('LOGIN');
  }
  if (auth.currentUser == null) {
    GoogleSignInAuthentication credentials =
        await googleSignIn.currentUser.authentication;
    await auth.signInWithGoogle(
      idToken: credentials.idToken,
      accessToken: credentials.accessToken,
    );
    print('CURRENTUSER');
  }
}

where I used the console outputs to verify which parts of the function are executed. The function call happens every time the app is started, by calling it in the main() function as

main() async{
     new LinearProgressIndicator(backgroundColor: Colors.lightGreen,);
     await _ensureLoggedIn();
     runApp(new SpringsterApp());
 }

I noticed however that although the Signin prompt with the available Google Accounts on the device happens every time the user has not signed in yet, the last part of the ensureLoggedIn function ( if (auth.currentUser == null) ) never gets executed, regardless of whether or not the user is signed in.

I noticed this since the print('CURRENTUSER');is never put out in the console, and additionaly because no new user is created in the Firebase Auth console. Does anyone know why this is happening and how I could maybe fix this?

My Firestore security rules are set to auth only, so no data can be written or read as long as this error occurs. Thanks in advance!

To anyone interested, I was able to sign in as planned with:

Future<Null> _ensureLoggedIn() async {
  GoogleSignInAccount user = googleSignIn.currentUser;
  if (user == null) user = await googleSignIn.signInSilently();
  if (user == null) {
    final GoogleSignInAccount googleUser = await googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth =
    await googleUser.authentication;
    final FirebaseUser user = await auth.signInWithGoogle(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );
    assert(user.email != null);
    assert(user.displayName != null);
    assert(!user.isAnonymous);
    assert(await user.getIdToken() != null);

    final FirebaseUser currentUser = await auth.currentUser();
    assert(user.uid == currentUser.uid);
  }
}

i already initilized firebase app. But still not connect to firebase auth. So i follow from pub dev docs firebase_auth .

ran on emulator.

bool shouldUseFirebaseEmulator = false; // before main()

if (shouldUseFirebaseEmulator) {
    await FirebaseAuth.instance.useAuthEmulator('localhost', 9099);
} // inside main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM