Home > Mobile >  Flutter FirebaseAuth: SignUp unable to handle error when the email address is used by another user
Flutter FirebaseAuth: SignUp unable to handle error when the email address is used by another user

Time:11-23

My code here, and the error pointed at -> await FirebaseAuth.instance .createUserWithEmailAndPassword( email: email.trim(), password: password.trim()); - in code below

 Future SignUp() async {
    try {
      UserCredential result = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(
              email: email.trim(), password: password.trim());
      User user = result.user;
      return user;
    } on FirebaseAuthException catch (e) {
      print(e);
      if (e.code == 'email-already-in-use') {
        setState(
            () // setState rebuilds the entire build by modifying the code inside it
            {
          error = e.toString();
          EmailExists =
              true; //sets the emailExists variable to 'true' and rebuild
        });
      }
    }
  }

Error: PlatformException (PlatformException(firebase_auth, com.google.firebase.auth.FirebaseAuthUserCollisionException: The email address is already in use by another account., {message: The email address is already in use by another account., additionalData: {}, code: email-already-in-use}, null))

CodePudding user response:

Try a different email because you already created a user with the email and password

CodePudding user response:

By default Firebase Authentication only allows a single user to register with a specific email address in a project. When a second user tries to register with the same email address, you get the error that you have and you'll need to handle that scenario in your code. Typically you'll tell the user that the email address has already been registered and either ask them for their password, or tell them to use another email address.

If you want to allow multiple users to register with the same email address, you can enable that by changing the Multiple accounts per email address setting in the Sign-in methods pabel of the Firebase console.

  • Related