6

Currently I need to set up a check whether a user is logged in or not to then act accordingly (open home or log in screen). I'm using only email authentication.

How to check user firebase auth state in flutter?

This question has already been asked here, but I failed to check auth state in this way:

final auth = FirebaseAuth.instance;

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'MyApp',
        home: (_checkLogin() == true ? new PostAuthScaffold() : new PreAuthScaffold())
    );
  }
}

bool _checkLogin() {
  return !(auth.currentUser == null);
}
Darkhan
  • 2,888
  • 5
  • 20
  • 30

2 Answers2

8

You can also check your auth status inside initState like so:

class CheckAuth extends StatefulWidget {
  @override
  _CheckAuthState createState() => new _CheckAuthState();
}

class _CheckAuthState extends State<CheckAuth> {
  bool isLoggedIn;
  @override
  void initState() {
    isLoggedIn = false;
    FirebaseAuth.instance.currentUser().then((user) => user != null
        ? setState(() {
            isLoggedIn = true;
          })
        : null);
    super.initState();
    // new Future.delayed(const Duration(seconds: 2));
  }

  @override
  Widget build(BuildContext context) {
    return isLoggedIn ? new Home() : new LoginScreen();
  }
}
Shady Aziza
  • 50,824
  • 20
  • 115
  • 113
  • returns "Failed assertion: boolean expression must not be null" error. Should I try to manually change isLoggedIn value every time user logs in or out and store the value in Shared Preferences? – Darkhan Apr 13 '18 at 12:46
  • That is because I forgot to initialize the value, I have update the code – Shady Aziza Apr 13 '18 at 12:56
5

What about

FirebaseAuth.instance.onAuthStateChanged.listen((user) {
  setState(() => isAuthenticated = user != null);
}) 
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • tried this method and also just in case with "user.email != null", but it doesn't seem to work - it still keeps returning to log in page after the app is reopened. Thank you – Darkhan Apr 13 '18 at 09:10
  • "still keeps returning to log in page after the app is reopened." why do you think this is related to the code I posted? – Günter Zöchbauer Apr 13 '18 at 09:12
  • I used isAuthenticated variable to open appropriate page: home: _isAuthenticated == true ? new HomeScreen() : new LogInScreen(). Is that a wrong way of doing it? – Darkhan Apr 13 '18 at 09:43
  • Difficult to say from that code snippet. It might be right, but you should test above code if it returns the expected result. – Günter Zöchbauer Apr 13 '18 at 10:01
  • yeah I can successfuly sign up and log in (thanks to your answer to my previous question), but if I close my app after I log in and then open it again - the log in screen pops up again (instead of home screen as expected) – Darkhan Apr 13 '18 at 10:57
  • Is the code in my answer executed when the login page shows? – Günter Zöchbauer Apr 13 '18 at 11:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168920/discussion-between-darkhan-and-gunter-zochbauer). – Darkhan Apr 13 '18 at 11:22