Home > Enterprise >  Persistent session after signout Flutter
Persistent session after signout Flutter

Time:11-25

I use Firebase Authentication for my app. I can log / register correctly except when another user was log previously.

Exemple : I am log, and I want to signout. Like this :

  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

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

IconButton(
                        onPressed: () {
                          signOut();
                          Navigator.of(context, rootNavigator: true)
                              .pushAndRemoveUntil(
                            MaterialPageRoute(
                              builder: (BuildContext context) {
                                return const OnBoardingPage();
                              },
                            ),
                            (_) => false,
                          );
                        },
                        icon: const Icon(Icons.logout))

So I came back to my onboarding page but I'm not fully disconnected.

I know it because I can display my email on the onboarding page (where normally no one can be connected)exemple

So, I need to restart the app, and then, I am no longer connected and user mail can't be displayed. I think it is something about cache but not sure.

I want to know how to fully disconnected my account of my app.

CodePudding user response:

Try

Future<void> signOut() async {
    await _firebaseAuth.signOut();
  }

and in onPressed func

onPressed: () async {
                          await signOut();
                          Navigator.of(context, rootNavigator: true)
                              .pushAndRemoveUntil(
                            MaterialPageRoute(
                              builder: (BuildContext context) {
                                return const OnBoardingPage();
                              },
                            ),
                            (_) => false,
                          );
                        },

CodePudding user response:

check the user currently signed in or not in splash screen

if ( FirebaseAuth.instance.currentUser != null) {
        // signed in
          return HomeScreen();
        } else {
         return LoginScreen();
        }

function to sign out

 Future<void> signOut(BuildContext context) async {
try {
 await FirebaseAuth.instance.signOut();
 Navigator.of(context).push(MaterialPageRoute(
     builder: (BuildContext context) =>  LoginScreen()));
}catch(e){
   print("signout err:" e.toString());
  }
}

}

  • Related