EnCoder2000
EnCoder2000

Reputation: 79

App doesn't remember that user is signed in

I currently managed to finish my web app and I tried to make it so that the user doesn't have to log in every time he/she accesses the app. This works in debugging mode but as soon as I deploy it on the actual web using firebase, every time you refresh the app it asks you to log in again. I've trying doing it using a Future builder and even check the current user. Everything works in debugging but on the real website that doesn't work.

Code:

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData.dark(),
        scrollBehavior: ScrollConfiguration.of(context).copyWith(dragDevices: {
          PointerDeviceKind.mouse,
          PointerDeviceKind.touch,
        }),
        home: FirebaseAuth.instance.currentUser != null ? Homepage() : Login());
  }
}

Upvotes: 0

Views: 840

Answers (3)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

Firebase automatically persists the user credentials in local storage, and restores them when the page reloads. This requires it to call to the server, to check amongst others whether the account has been disabled.

While this call to the server is going on currentUser is null, which is why your code sends the user to the login page when it loads. When the call from the server completes/returns, Firebase updates the currentUser, but at this point your rendering code is not retriggered.


What you'll want to do is listen the authStateChanges() stream as shown in the documentation on authentication state. By putting this in a StreamBuilder, your UI will respond to the changes in authentication state. For some examples of this, see How to manage Firebase Authentication state in Flutter?


With this approach, the UI may briefly show the login page (during the call to the server) and then show the correct page once the state has been restored. If you want to prevent this brief flash, you what androloper did in their answer. Just make sure to do it in addition to listening to authStateChanges, as only checking the local state would mean you're missing cases where the server rejects the stored credentials.

Upvotes: 2

Vishal_VE
Vishal_VE

Reputation: 2127

Just Use Shared Preference for store any value which identify that State... Then remove the Preference key at the time of logout...

So when the app start check that store value is exist.. IF exist then its logged otherwise Logged out...

Upvotes: 0

androloper
androloper

Reputation: 68

I have faced with this problem. I havent used google firebase but it is about checking token. Maybe this one helps you to handle it. Firstly, you should create future, such as:

Future<bool> autoLogin() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool accessToken = prefs.containsKey('accessToken');
  return accessToken;
}

then you call it like this:

home: FutureBuilder<bool>(
          future: autoLogin(),
          builder: (context, snapshot){
            if(snapshot.data == null){
              return SplashScreen();
            }else {
              return snapshot.data == true ? WantedToGoScreen() : LoginScreen();
            }
          },
        ),

Upvotes: 0

Related Questions