I can sign in and I'm not calling the signOut()
method of FirebaseAuth
anywhere in my app.
Despite this, I'm signed out every time I quit (e.g. with q
) and re-run the app with flutter run
.
Is there anything in particular that I should check to solve this problem?
Here's some code for my Firebase setup:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
And here is some of my AuthNotifier
, in which I listen to the auth state changes and print them:
class AuthNotifier extends ChangeNotifier {
final FirebaseAuth _auth;
AuthNotifier({FirebaseAuth? auth}) : _auth = auth ?? FirebaseAuth.instance {
listenToAuthChanges();
}
void listenToAuthChanges() {
_auth.authStateChanges().listen((User? user) {
debugPrint("<< User is signed out: ${user == null}");
// Outputs "<< User is signed out: true" (unexpected) whenever I run the app,
// "<< User is signed out: false" (expected) when I sign in.
});
}
}
Restarting VS Code and my computer didn't help.
macOS 15.0, MacBook Air M1 8GB.
Chrome 131.0.6778.86.
Flutter 3.24.4.
Dart 3.5.4.
firebase_auth
5.3.3.
VS Code 1.95.3.
This issue is not because of firebase, but because of how flutter is run on the web. The chrome instance that is spawned, is totally different from how you regularly run chrome. The state of the new chrome instance does not persist when that instance is closed.
If you want to persist your login details, you can run the flutter app as a web server using
$ flutter run -d web-server
You can open the URL in your browser, and thus whenever you relaunch the Flutter app, the firebase login data will persist.