Why Firebase email verification not working with Gmail accounts?

1,035

I was having the same issue and I discovered that verification emails were sent but gmail treated then as spam email. Please check in the spam folder

Share:
1,035
Anas Ansari
Author by

Anas Ansari

Updated on January 05, 2023

Comments

  • Anas Ansari
    Anas Ansari about 1 year

    I am trying to implement email verification using Firebase. And It's not sending a verification email to a Gmail address like [email protected] but working fine with other email addresses like temp-mail. I was watching Johannes Milke's tutorial for it, I came across this issue.

    I'm not able to understand why it's happening. Please help.

    Code for verification page

    class VerifyEmailPage extends StatefulWidget {
      const VerifyEmailPage({Key? key}) : super(key: key);
    
      @override
      State<VerifyEmailPage> createState() => _VerifyEmailPageState();
    }
    
    class _VerifyEmailPageState extends State<VerifyEmailPage> {
      bool isEmailVerified = true;
      Timer? timer;
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
    
        //User need to be created before
    
        isEmailVerified = FirebaseAuth.instance.currentUser!.emailVerified;
        if (!isEmailVerified) {
          sendVerificationEmail();
          timer = Timer.periodic(const Duration(seconds: 3), (timer) {
            checkEmailVerified();
          });
        }
      }
    
      Future checkEmailVerified() async {
        FirebaseAuth.instance.currentUser!.reload();
        // user.reload;
        setState(() {
          isEmailVerified = FirebaseAuth.instance.currentUser!.emailVerified;
        });
        if (isEmailVerified) timer?.cancel();
      }
    
      @override
      void dispose() {
        // TODO: implement dispose
        timer?.cancel();
        super.dispose();
      }
    
      Future sendVerificationEmail() async {
        try {
          final user = FirebaseAuth.instance.currentUser;
          // await firebase.auth().currentUser.sendEmailVerification();
    
          await user
              ?.sendEmailVerification()
              .then((value) => print("Verification sent! to ${user.email}"));
        } on Exception catch (e) {
          // TODO
          showErrorAlert(context, e.toString());
        }
      }
    
      AuthService _auth = AuthService();
    
      @override
      Widget build(BuildContext context) => isEmailVerified
          ? HomeScreen(selectedIndex: 0)
          : Scaffold(
              appBar: AppBar(
                title: Text("Verify your email"),
                centerTitle: true,
                elevation: 0,
                backgroundColor: kPrimaryColor,
                leading: IconButton(
                  icon: Icon(Icons.arrow_back),
                  onPressed: () async {
                    await _auth.signOut();
                  },
                ),
              ),
              body: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                // crossAxisAlignment: CrossAxisAlignment.center,
                children: const [
                  Text("Verification email is sent to you!!",
                      // textAlign: TextAlign.center,
                      style: TextStyle(fontSize: 20.0),
                      textAlign: TextAlign.center),
                ],
              ));
    }
    
    • Ahmed Adel
      Ahmed Adel almost 2 years
      Did you check the junk folder, sometimes verification mails are being filtered as junk
  • trm
    trm over 1 year
    Lost a whole day googling and never find answers, finally found this... Thanks.