Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Signout a user in Flutter with Firebase authentication

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.

like image 448
Shady Aziza Avatar asked Jul 13 '17 11:07

Shady Aziza


People also ask

How do I remove a user from Firebase authentication?

You can also delete users from the Authentication section of the Firebase console, on the Users page. Important: To delete a user, the user must have signed in recently. See Re-authenticate a user.

Which method will you call to logout a user from Firebase?

If you'd like to sign the user out of their current authentication state, call the signOut method: import auth from '@react-native-firebase/auth'; auth() . signOut() .


1 Answers

Firebase auth's signOut method is asynchronous. You should make your _signOut method async.

Future<void> _signOut() async {   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.

like image 77
Collin Jackson Avatar answered Sep 28 '22 07:09

Collin Jackson