How to Signout a user in Flutter with Firebase authentication

aziza picture aziza · Jul 13, 2017 · Viewed 24k times · Source

I have a problem signing out the current user from my app

the method I am using is as follows:

....
onPressed:_signOut
//jump to function


  void _signOut() {
  FirebaseAuth.instance.signOut();
  FirebaseUser user = FirebaseAuth.instance.currentUser;
  //print('$user');
  runApp(
      new MaterialApp(
        home: new LoginPage(),
      )

  );
}

So now when I press the button it should sign the user out and redirect them to the home page which they will have to login again, however, the redirecting happens but the user data would still be saved so when I press the button again it automatically sign in again with the last account. How can I remove user data so the app asks about their credentials each time they try to login after a logout ?

I feel I am missing something in the linkage between pages and how their behavior change accordingly, but what is it ?

Update: I use google sign in function with firebase authentication

   Future<String> _testSignInWithGoogle() 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(!user.isAnonymous);
  assert(await user.getToken() != null);
return 'signInWithGoogle succeeded: $user';
}

My login page looks like this:

    class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return new Scaffold(
        appBar: new AppBar(
          title: new Text("Login"), backgroundColor: Colors.blue,),
        body: new Container(
            child: new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    new IconButton(
                      icon: new Icon(Icons.account_box, color: Colors.red),
                      onPressed:  _signIn,
                      iconSize: 80.0,),
                    new Text("Google Signin")
                  ],
                )
            )
        )
    );
  }
}

Update: Changed _signOut() method to be async as follows:

    Future <LoginPage> _signOut()  async{
    await FirebaseAuth.instance.signOut();

    return new LoginPage();
}

Now when I press on signout, it does not redirect me to the LoginPagae nor does it sign the user out.

Answer

Collin Jackson picture Collin Jackson · Jul 13, 2017

Firebase auth's signOut method is asynchronous. You should make your _signOut method async and call await FirebaseAuth.instance.signOut(); so that the call to runApp occurs after the user is signed out.

You should also call _googleSignIn.signOut() when logging out if you want signIn to present the user with an authentication dialog instead of silently and automatically re-using the current Google user.