Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flutter app force a user to choose an account with FirebaseAuth and GoogleSignInAuthentication

I want to force a user to select one of his account during login time. Is there any method to do so? I haven't found any configuration like prompt=select_account+consent.

Now, with these codes, after a user logout and then try to login again, it will automatically sign in with the selected account, there is no window showing up for user to select an account.

pubspec.yaml

firebase_auth: ^0.8.1+4
google_sign_in: ^3.2.4

Login part

GoogleSignInAccount googleUser = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth = await googleUser.authentication;

final AuthCredential credential = GoogleAuthProvider.getCredential(
  accessToken: googleAuth.accessToken,
  idToken: googleAuth.idToken,
);
user = await _auth.signInWithCredential(credential);

Logout part

await FirebaseAuth.instance.signOut();
GoogleSignIn _googleSignIn = GoogleSignIn();
await _googleSignIn.signOut();
like image 604
Watchanan Avatar asked Mar 17 '19 10:03

Watchanan


2 Answers

Use GoogleSignInAccount.disconnect() before signing out to revoke the previous authentication:

await _googleSignIn.disconnect();
await FirebaseAuth.instance.signOut();
like image 76
Harold Aldous Sarmiento Avatar answered Oct 07 '22 13:10

Harold Aldous Sarmiento


Harold's answer used to work for me, but recently the GoogleSignIn().currentUser appears null for some devices I tested, and then the disconnect function won't work. So, what solved that problem was ensuring it is signed in to Google.

final googleCurrentUser =
        GoogleSignIn().currentUser ?? await GoogleSignIn().signIn();
    if (googleCurrentUser != null)
      await GoogleSignIn().disconnect().catchError((e, stack) {
        FirebaseCrashlytics.instance.recordError(e, stack);
      });
    await _auth.signOut();
like image 32
Hyung Tae Carapeto Figur Avatar answered Oct 07 '22 13:10

Hyung Tae Carapeto Figur