skip to Main Content

Normally, the email verification has been sent to the user after creating the account using UserCredential userCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword( email: email, password: password, ); in Flutter.

I want to know: is it possible to create a user account in Firebase ,only if the verification is successful?

2

Answers


  1. Yes possible.

    Future<void> registerUser(String email, String password) async {
      try {
        UserCredential userCredential = await FirebaseAuth.instance
            .createUserWithEmailAndPassword(email: email, password: password);
    
        // Send email verification
        await userCredential.user.sendEmailVerification();
    
        // Check if the email is verified before allowing further actions
        if (!userCredential.user.emailVerified) {
          // Handle the case where email verification is pending
          // You might log the user out and ask them to verify their email before signing in again
        } else {
          // Continue with your app flow for a verified user
          // For example, you might automatically sign in the user here
        }
      } catch (e) {
        // Handle any registration errors here
        print("Error registering user: $e");
      }
    }
    

    check this ant this also.

    Login or Signup to reply.
  2. It’s not possible the way you are describing it. Once you run createUserWithEmailAndPassword, that account is created.

    What you can do instead is write some code using the Firebase Admin SDK to periodically delete the accounts that were never verified after they have been unverified for some amount of time. Perhaps you could use a scheduled function to run this sort of process daily. You shouldn’t depend on running this logic in the client app since you have no guarantee that it will continue to run after the account is created.

    Also consider using an email link to create the user account, as you can bundle the creation of the account with email verification in one call.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search