0
votes

i'am building an app with flutter and firebase..(I'am new in flutter development). What I did is a system where the user can signup and signin. Once the user signup an email verification is sent to the user email account. I'll try to put all the step below

  1. signup
  2. redirect to email verification widget..(this check if user has verified the email with a Timer) if yes, navigator push new Page (HomePage).

the main logic is this one.

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
  return MaterialApp(
    title: "My App Name",
    debugShowCheckedModeBanner: false,
    home: AuthService().handleAuth(),
    theme: ThemeData(
      visualDensity: VisualDensity.adaptivePlatformDensity
    ),
  );
 }}

this is what the AuthService().handleAuth() does:

handleAuth() {
    return StreamBuilder(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: (BuildContext context, snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
          print(snapshot.hasData);
          if (snapshot.hasData && emailVerificationNeeded() == false) {
            return HomePage();
          }
          return LoginPage();
        } else {
          return Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
      },
    );
  }

The verifyPage check with a timer if user has verified the email

Future<void> checkEmailVerified() async {
    user = auth.currentUser!;
    await user.reload();
    if (user.emailVerified) {
      timer.cancel();

      Navigator.of(context)
          .pushReplacement(MaterialPageRoute(builder: (context) => HomePage()));
    }
  }

Until here everything work fine! Now for a test purpose in the HomePage there is a button that fire the following action

FirebaseAuth.instance.signOut();

if I click in the button and I logout the user still remain in the home page instead of going back to the LoginPage. This problem happen only the first time when the user is redirected in the Verification page. On all the other case if I'm verified and I'm logged in, once I click on the Logout button the user is redirect back to the Login page.

Any ideas?

Thank you all

1

1 Answers

1
votes

The StreamBuilder isn't reacting to changes because you've navigated to another route.

You can use FirebaseAuth.instance.userChanges instead of FirebaseAuth.instance.authChanges as authChanges only "notifies about changes to the user's sign-in state (such as sign-in or sign-out)." while userChanges listens to different states such as sign-in, sign-out, different user & token refresh.

And then you can call FirebaseAuth.instance.currentUser!.reload(); when you verify the user which removes the need to navigate to the HomePage manually.

So when you call FirebaseAuth.instance.signOut, it automatically signs you out.

Code Changes:

Update handleAuth to this:

handleAuth() {
    return StreamBuilder(
      stream: FirebaseAuth.instance.userChanges(),
      ...
    );
}

Update checkEmailVerified to this:

Future<void> checkEmailVerified() async {
    user = auth.currentUser!;
    await user.reload();
    if (user.emailVerified) {
      timer.cancel();
    }
}