CSFrequency / react-firebase-hooks

React Hooks for Firebase.
Apache License 2.0
3.58k stars 305 forks source link

CreateUserWithEmailAndPassword doesn't return error for existing users #220

Closed metacoding closed 1 year ago

metacoding commented 2 years ago

If there is a user with the same email registered, the call will continue and then() method will be called. There is an HTTP error 400 visible in the console log that is not returned as an error (User Exists)

Example code:

const handleSignup = () => {
    const newUser: CoreUser = {
      email: watch("email"),
      password: watch("password"),
    };
    if (createUserWithEmailAndPassword) {
      createUserWithEmailAndPassword(newUser.email, newUser.password)
        .then((result) => {
          sendEmailVerification()
            .then((result) => {
              console.log(`Email was sent to ${newUser.email} `);
            })
            .catch((error) => {
              console.error(
                `error while sending email: ${error.message}`,
                error
              );
            });
          dispatch(userSignUpSuccess());
        })
        .catch((error) => {
          dispatch(userSignUpError(error));
        });
    }
  };

Console log: CleanShot 2022-03-11 at 06 34 53

Error returned visible in network section:

{
    "error": {
        "code": 400,
        "message": "EMAIL_EXISTS",
        "errors": [
            {
                "message": "EMAIL_EXISTS",
                "domain": "global",
                "reason": "invalid"
            }
        ]
    }
}
chrisbianca commented 1 year ago

@metacoding this was an oversight due to the way that the hooks catch errors.

As of v5.1.0 which has just been released, createUserWithEmailAndPassword will return an auth.UserCredential if successful or undefined if not. You'll want to check this before calling sendEmailVerification.