Is this the right way to check if user is logged in? Flutter firebase

Viewed 1138

I was adding login/register in my app using Firebase. It is working perfectly, but now i wanted to check if the user has already registered and logged in when he opens the app, if that's true then show him the Home page, else show him the Login page. So i did some searching and mostly it's done using the Streambuilder method. But all i did was to check if the user is logged in using

initialRoute: FirebaseAuth.instance.currentUser == null
              ? Routes.loginScreen
              : Routes.homePage,

and it works perfectly, Almost everyone is using the Streambuilder method. So i was curiois am i doing it the wrong way? Is there a better way to do this? Here's the full code btw. Which is working fine, but i just want to know if it's the right way?

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: _initialization,
      builder: (context, snapshot) {
        if (snapshot.connectionState != ConnectionState.done) {
          return Center(
            child: CircularProgressIndicator(),
            //Todo Add splashscreen
          );
        }
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: "MyApp",
          initialRoute: FirebaseAuth.instance.currentUser == null
              ? Routes.loginScreen
              : Routes.homePage,
          onGenerateRoute: Routes.generateRoute,
        );
      },
    );
  }
}
1 Answers

The reason for using streambuilder, is to monitor changes and actually reflect them on your app. This code:

FirebaseAuth.instance.currentUser == null
              ? Routes.loginScreen
              : Routes.homePage,

Works properly but only once, when your app runs. It captures the auth state only when your app reaches that line of code. But if afterwards the user is signed out, the app UI does not change, and here is where the added benifit of using a stream comes. If anywhere in your app down the line, you add a sign out button, your user will indeed be logged out, but will still be inside your app. The login screen will not be returned.

Since you are using FirebaseAuth SDK, it's recommended to make the best out of it, as it comes with built in stream such as idTokenChanges , authStateChanges, and userChanges, they will make your life much easier, and your app code more elegant.

If you want to keep your code like this, it's fine, but make sure that in your loginScreen route, you have a stream, which listens to Auth changes and a pseudo code similar to something like this:

StreamBuilder<User?>(
          stream: FirebaseAuth.instance.userChanges(),
          initialData: FirebaseAuth.instance.currentUser,
...etc
 if (snapshot.data == null) 
     return LoginScreenLogicWithButtons());
           
 if (snapshot.data != null) 
     return  HomePage();
Related