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

5,780

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"));
});
Share:
5,780
phuocding
Author by

phuocding

Updated on December 08, 2022

Comments

  • phuocding
    phuocding over 1 year

    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;
        }
      }
      ...
    
  • phuocding
    phuocding over 5 years
    Awesome! I forgot I just signout authentication, not sign out the account. Thanks so much
  • phuocding
    phuocding about 5 years
    how to sign out immediately after close app or exit app?