Flutter - Google Sign In uses previously signed in account (user) even after signing out and signing in with another account

Viewed 720

When I hit "Sign in with Google", it does let me choose the account although somehow, the previous user gets loaded.

    class GoogleSignInProvider extends ChangeNotifier {
  GoogleSignIn? _googleSignIn;
  GoogleSignInAccount? _user;
  GoogleSignInAccount get user => _user!;
  bool _isGoogleLogin = false;
  final _auth = FirebaseAuth.instance;
  Future signInWithGoogle(context) async {
   
    try {
      _googleSignIn = GoogleSignIn();
      final googleUser = await _googleSignIn!.signIn();
      if (googleUser == null) {
        return;
      }
      _user = googleUser;
      final googleAuth = await googleUser.authentication;

      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleAuth.accessToken,
        idToken: googleAuth.idToken,
      );


      var authCredential = await _auth.signInWithCredential(credential);
      _isGoogleLogin = true;

      if (authCredential.user != null) {
        final snapshot = await FirebaseFirestore.instance
            .collection('users')
            .doc(authCredential.user!.uid)
            .get();

        if (!snapshot.exists) {
          var userId = authCredential.user!.uid;
          FirebaseFirestore.instance.collection('users').doc(userId).set({
            "email": googleUser.email,
            "workoutsCount": 0,
            "lastWorkoutDate": null
          });
        }
      }
      notifyListeners();

      Navigator.of(context).pushNamed('/dashboard');
    } on FirebaseAuthException catch (e) {
      showSnackBar(context, e);
    } catch (e) {
      print('ERROR = ');
      print(e);
    }
  }

  Future<void> signOut(context) async {
    try {
      if (_isGoogleLogin) {
        final googleCurrentUser =
            GoogleSignIn().currentUser ?? await GoogleSignIn().signIn();
        if (googleCurrentUser != null) {
          await GoogleSignIn().disconnect().catchError((e, stack) {
            FirebaseCrashlytics.instance.recordError(e, stack);
          });
        }
      }
      await _auth.signOut();

    } catch (e) {
      showSnackBar(context, e);
    } finally {
      _isGoogleLogin = false;
      Navigator.of(context).pushNamedAndRemoveUntil(
          '/register_login', (Route<dynamic> route) => false);
    }
  }

Is there something wrong with this code??

4 Answers

Can you try to disconnect the account before signing out:

GoogleSignIn _googleSignIn = GoogleSignIn();
await _googleSignIn.disconnect();
await FirebaseAuth.instance.signOut();

Remove if (_isGoogleLogin) from your signOut function. googleCurrentUser will check the consitionn and responses based on the result.

First Create a Global variable

FirebaseAuth firebaseAuth = FirebaseAuth.instance;
GoogleSignIn googleSignIn = GoogleSignIn();

Make sure you are using the above variable for login and logout

final googleCurrentUser = firebaseAuth.currentUser;
  if (googleCurrentUser != null) {
    googleSignIn.signOut();
    await firebaseAuth.signOut();
  }

User above code for signout user

Related