I am using Firebase Authentication on my Flutter Web app, but the session is not persisted during refresh.
This is the package I am using.
https://pub.dev/packages/firebase
This is how I am authenticating with Firebase
static Future<User> handleSignInEmail(String email, String password) async {
await init();
final UserCredential userCredential =
await auth().signInWithEmailAndPassword(email.trim(), password);
assert(userCredential.user != null);
assert(await userCredential.user.getIdToken() != null);
final User currentUser = await userCredential.user;
assert(userCredential.user.uid == currentUser.uid);
print('signInEmail succeeded: $userCredential.user');
return userCredential.user;
}
If I refresh the page and call the following method, the user that is returned is null:
static Future<User> getFirebaseUser() async {
await init();
return await auth().currentUser;
}
A similar implementation using Flutter Mobile works as expected. What am I missing on the Flutter Web implementation?
Note that Firebase Auth web sessions are single host origin and will be persisted for a single domain only. Indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed.
Flutter works with existing code, is used by developers and organizations around the world, and is free and open source. In this lab, you will create a Firebase Meetup application. The application will demonstrate how to use Firebase Web authentication in a Flutter application.
Login happens automatically and it is handled by Firebase, however in async way. More details can be found in the official documentation:
https://firebase.google.com/docs/auth/web/auth-state-persistence
The trick is very simple. You need to wait for the first state change.
static Future<User> getFirebaseUser() async {
await init();
//return await auth().currentUser;
return await auth().onAuthStateChanged.first;
}
It returns null
if there is no signed-in user:
https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#onauthstatechanged
My firebase
version:
firebase: 5.0.4 #https://pub.dartlang.org/packages/firebase
I wrote this implementation which works on both mobile and web:
static Future<FirebaseUser> getFirebaseUser() async {
FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
if (firebaseUser == null) {
firebaseUser = await FirebaseAuth.instance.onAuthStateChanged.first;
}
return firebaseUser;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With