0

Well I am designing a sign up page and collecting some user datas such as phone number, name, surname, password etc. I want to add these infos to Firestore after user verifies e mail. User being created but the infos are not being added into Firestore. When I removed the if statement if (userCredential.user!.emailVerified == true)and use only addUserDetails function it works perfectly. So my question is how can I check that state and write it into my code.

try {
           
            final userCredential =
                await FirebaseAuth.instance.createUserWithEmailAndPassword(
              email: _emailController.text.trim(),
              password: _passwordController.text.toString(),
            );
           
           
            
            await userCredential.user?.updateDisplayName(
                "${_firstNameController.text.toString()} ${_lastNameController.text.toString()}");
            await userCredential.user?.updateEmail(_emailController.text.trim());
    
            if (userCredential.user!.emailVerified == true) {
              addUserDetails(
                _firstNameController.text.trim(),
                _lastNameController.text.trim(),
                _emailController.text.toLowerCase(),
                _phoneNumberController.text.trim(),
                _passwordController.text.toString(),
                FirebaseAuth.instance.currentUser!.uid.toString(),
              );
            }
          }





      Future addUserDetails(String firstName, String lastName, String eMail,
          String phoneNumber, String password, String userID) async {
        await FirebaseFirestore.instance
            .collection("users")
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .set({
          "First Name": firstName,
          "Last Name": lastName,
          "E-mail": eMail,
          "Phone Number": phoneNumber,
          "Password": password,
          "User ID": userID,
        });
      }

1 Answers1

2

It sounds like userCredential.user!.emailVerified is never true when you perform the check.

Keep in mind that email verification happens out of band (it happens in another app, typically the email client or the default web browser) and your client-side application code doesn't get a notification when it happens. For more on this, see:

One possible workaround is to have a loop that periodically:

  • Reloads the user profile from the server.
  • Checks if the email is now verified.
  • If so, writes the information to Firestore.

Another alternative is to just always write the information to Firestore, including whether the email is verified, then update that when the app is reloaded after checking if the user's email address is now verified.

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