snowyvibes
snowyvibes

Reputation: 296

User is always signed in and can't logout in flutter firebase

Main Dart:

  Widget build(BuildContext context) {
    return StreamProvider<MyUser?>.value(
        value: AuthService().user,
        initialData: null,
        child: MaterialApp( //code for material dart

Wrapper Widget:

Widget build(BuildContext context) {
    final user = Provider.of<MyUser?>(context);
    print(user);
    if (user == null) {
      print('no one logged in');
    } else {
      print(user.uid);
    }
    return user == null ? LoginPage() : NavigationBar();
  }

So I'm trying to load the login page whenever user is null, but I only logged in the first time and now it stays logged in even after deleting that account from firebase console.

Output after hot restart:

Restarted application in 935ms.
flutter: null
flutter: no one logged in
flutter: Instance of 'MyUser'
flutter: ETBVvt2A0JggH8KSrgdLh3DP8cG3

I don't get why the first time it returns 'no one logged in' but it runs a second time too and returns the uid of the user that is logged in.

The logout button doesn't work either. It does't return an error or anything, it just doesn't produce any output.

Here is the signOut button method:

//method
  final FirebaseAuth _auth = FirebaseAuth.instance;

  Future signOut() async {
    try {
      return await _auth.signOut();
    } catch (error) {
      print(error.toString());
      return null;
    }

Signout button:

 onPressed: () async {
                        dynamic result = await _auth.signOut();
                        if (result == null) {
                          print('error signing out');
                        } else {
                          print('signed out');
                          Navigator.pushReplacementNamed(
                              context, LoginPage.routeName);
                        }
                      },

Upvotes: 1

Views: 3265

Answers (3)

Dhiraj Singh
Dhiraj Singh

Reputation: 29

Please call this function in your oppressed area

Future<Null> _signOut() async { await GoogleSignIn().signOut(); print("logout Successfully"); }

Here GoogleSignIn(), is the function where you define your signin codes

Upvotes: 0

snowyvibes
snowyvibes

Reputation: 296

Apparently the problem wasn't with firebase but with the custom button I had built. I didn't assign the onPressed properly and the logout button wasn't working because of that.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Firebase automatically persists the user credentials locally, and tries to restore those when the app is restarted.

To sign the user out, you have to explicitly sign the user out by calling await FirebaseAuth.instance.signOut().

Upvotes: 1

Related Questions