Home > Back-end >  firebase_dart automatically sign in
firebase_dart automatically sign in

Time:07-01

I am coding an app running on iOS, Android, macOS, Linux and Windows which uses the Firebase Auth and Firebase Realtime Database (Or Firestore Database). Because Windows is not yet supported by the official firebase packages, I am using firebase_dart.

After implementing the sign in, I found out that if I restart the app, I have to sign in again. I think this is because the package does not store the user identity / token / state. So I need to manually store it but I don't know what to store.

final FirebaseAuth _auth;
...

// These are the credentials I receive from the api call
final credentials = await _auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );

Does someone know how to stay signed in with this package?

CodePudding user response:

Firebase automatically saves your user session when you sign in the regular way (with email/password or through other credentials), and you can use the userChanges() listener to see if the user is signed in. It will be fired with the user or null depending whether the user has signed in before (and you have no explicitly called signOut().

_auth.userChanges().listen((user) {
  debugPrint('user changed $user');

  if (user != null) {
    // do stuff with user data
  } else {
    // if you need to handle no user on start up
  }
});
  • Related