How to Signout a user in Flutter with Firebase authentication

Viewed 57387

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.

7 Answers

You need to have an Instances of FirebaseAuth

  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

And Then

  _signOut() async {
    await _firebaseAuth.signOut();
   }

Most demos I've looked at only logout of Firebase using a command like _auth.signOut();

This doesn't appear to exist anymore (see Collin's reply above):

_googleSignIn.signOut()

So, I had to use this one method to signout/logout of Google.

_googleSignIn.disconnect();

First create an instance of FirebaseAuth like so

 FirebaseAuth auth = FirebaseAuth.instance;

Then add this to either your logout button or any means you wish to use for the logout.

signOut() async {
    await auth.signOut();
  }

You can also create a function and then call the signOut within your button like so

import 'package:flutter/material.dart';

class SignOut extends StatefulWidget {
  @override
  _ SignOut State createState() => _ SignOut State();
}

class _ SignOut State extends State< SignOut > {

     FirebaseAuth auth = FirebaseAuth.instance;
    
      signOut() async {
        await _firebaseAuth.signOut();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.white,
          body: Center(
            child: Container(
              child: RaisedButton(
                onPressed: () {
                  signOut();
                },
    
              )
            ),
          ),
        );
      }
    }

You decide.

If every answer above is not working, maybe a Page is laying above everything else, then use Navigator.of(context).pop(); to pop the page.

I've searched hours looking for a mistake that wasn't even there just for this small mistake.

Declare a Future method to call logout function and redirect your page when logout is finished:

Future logout() async {
await _firebaseAuth.signOut().then((value) => Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => LoginPage()),(route) => false))); 
}

you should make your signout function asynchronous

   Future<void> _signOut() async {
  await FirebaseAuth.instance.signOut();
}
Related