Flutter Authentication sign in & sign out with Google from logged screen to login screen

Viewed 6134

today I tried to build 2 screens "login with GG" and "logout redirect to login screen". Succeed! But when I log in again, Previous gg account logged in immediately without requiring sign-in popup. On my device remember logged account. How to sign out completely and sign-in again with the other accounts. Here my code: I have 'the home page with login with google' and 'home-center page with signout button'. Besides, I have api.dart to log in and log out, and the main page using routes to 2 pages. - Main Page:

routes: {
    "home-page": (context) => MyHomePage(),
    "game-center": (context) => GameCenterPage(),
  },
  home: MyHomePage(),

- Api.dart:

class FBApi {
  static FirebaseAuth _auth = FirebaseAuth.instance;
  static GoogleSignIn _googleSignIn = GoogleSignIn();

  FirebaseUser firebaseUser;

  FBApi(FirebaseUser user) {
    this.firebaseUser = user;
  }

  static Future<FBApi> signInWithGoogle() async {
    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(await user.getIdToken() != null);

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

    return FBApi(user);
  }

  static Future<void> signOut() async {
    await _auth.signOut().then((_) {
      print("***** log out...what the hell?");
      // Navigator.of(context).pushNamedAndRemoveUntil("/login", 
ModalRoute.withName("/home"));
    });
  }
}

- gameCenter.dart:

onPressed: () async {
          _signOut();
          Navigator.of(context).pushNamedAndRemoveUntil("home-page", ModalRoute.withName("game-center"));
        },

- home.dart

class MyHomePage extends StatelessWidget {
  Future<bool> _loginUser() async {
    final api = await FBApi.signInWithGoogle();
    if (api != null) {
      return true;
    } else {
      return false;
    }
  }
  ...
3 Answers

I experienced a similar issue this weekend and resolved it by also signing out of GoogleSignIn - however I'm not sure if this is the correct approach as I would have expected FirbaseAuth to auto sign out of all providers.

static Future<void> signOut() async {
  await _auth.signOut().then((_) {

  //try the following
  _googleSignIn.signOut();
  //try the following

  Navigator.of(context).pushNamedAndRemoveUntil("/login", ModalRoute.withName("/home"));
});

if you are having issue with the login popup for Google SignIn do this after signout call

 Navigator.pushReplacement(
        (context), MaterialPageRoute(builder: (context) => widget));

User gets signed out from Firebase, but not from Google.
So next time you already have a user signed in and that is returned by GoogleSignIn().
You need to sign out from Google as well. Following works perfectly for me:

Future signOut() async {
    try {
      await _fireBaseAuthInstance.signOut();
      await _googleSignInInstance.signOut();
    } catch (e) {
      print('Failed to signOut' + e.toString());
    }
}
Related