0

I have a Flutter app where I let users sign up with only their email address. It should of course be verified. When sending the request the mail arrives and the link works. It even says "Your email has been verified". For some reason the user in the app is still not registered as verified though.

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: AuthService().userStream,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const LoadingScreen();
        }
        if (snapshot.hasError) {
          return const Scaffold(
            body: Padding(
              padding: EdgeInsets.all(10),
              child: ErrorScreen(),
            ),
          );
        } else if (snapshot.hasData) {
          if (snapshot.data!.emailVerified) {
            return const OverviewScreen();
          }
          return Scaffold(
            body: Center(
                child: Column(
              children: [
                const Spacer(),
                const Spacer(),
                const Text('Verify yourself'),
                const Spacer(),
                ElevatedButton(
                  onPressed: () {
                    AuthService().user!.sendEmailVerification();
                  },
                  child: const Text('Resend verification email'),
                ),
                const Spacer(),
              ],
            )),
          );
        } else {
          return const RegisterScreen();
        }
      },
    );
  }
}

AuthService().userStream is just

FirebaseAuth.instance.authStateChanges();

And AuthService().user is just

FirebaseAuth.instance.currentUser;

This question here tackles a similar problem, but even after restarting the app, the user's still not verified. Using the StreamBuilder should prevent manual reloading anyway.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

1

Verifying the email address happens outside of your application, so the app doesn't automatically pick up the change in the user profile. It picks up this change:

  • When the user logs in again.
  • When the SDK automatically refreshes the ID token of the current user, which happens once per hour.
  • When you explicitly call reload on the user.

Restarting the app seems to do none of these in your case, so that explains why you don't see the new status. The most common approaches are to log the user out and in again, or to periodically reload their profile - for example when the app regains focus.

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807