[SOLVED] Flutter Web Firebase Auth’s persistence doesn’t work on PWA

Issue

This Content is from Stack Overflow. Question asked by F.SO7

I have developed a Flutter Web app that uses Firebase Authentication in order to sign in users to the app.

I’ve declared the Firebase Authentication persistence field so that the app will remember and auto-login the user when he revisits the Flutter Web app’s URL, and won’t be required to re-login every time he launches the URL.

It all works fine on a regular browser, but when the user generates a PWA (for example, clicking “Add to Home Screen” on iOS devices to save the website as PWA), the persistence feature stops working, and the user is required to re-login every time he opens the PWA.

Is there a way to add Firebase Authentication’s persistence feature to a PWA? And if not, is there a way to prevent generating a PWA (and saving the Flutter Web app as a regular browser URL when clicking “Add to Home Screen” button on iOS, for example)?

Thank you!



Solution

To solve the persistence problem, add a listener:

FirebaseAuth.instance.idTokenChanges().listen((User? user) async {
  if (user == null) {
    // Function for user not logged in here. Do not write function to change page here.
  } else {
    // As it's a Future it will take a while to process the user's information, so it 
       will call the function after it's done.
    Navigator.pushReplacement(
        context, MaterialPageRoute(builder: (_) => Home()));
  }
}

This is an example I made and it worked, use controllers to change the status, put some function to wait for the information to be processed.

Hope this helps. Any questions, at your disposal.


This Question was asked in StackOverflow by F.SO7 and Answered by Jéf Quadra It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?