I have an app that can't persist the user state of firebase auth for security reasons but I can't find a way to logout the user when the app is closed or force closed.
I'm using a listener to navigate the user to next page but, with this, when I open the app it automatically navigate to the logged page.
Future<StreamSubscription<User?>> listenUserChanges(
GlobalKey<NavigatorState> navigatorKey) async {
final sub = auth.userChanges().listen((event) async {
if (event != null) {
final token = await getUserId();
await storage.setToken(token);
navigatorKey.currentState!.pushReplacementNamed('/logged');
} else {
await storage.removeToken();
}
});
return sub;
}