How can I check if user exists in Firebase?

The error that is returned from method createUserWithEmailAndPassword has a code property. Per the documentation the error code auth/email-already-in-use:

Thrown if there already exists an account with the given email address.

At very minimum you can utilize conditional statements such as if/else or switch to check for that code and display/log/dispatch/etc a message or code to the user:

fire.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)
  .then(u => {})
  .catch(error => {
     switch (error.code) {
        case 'auth/email-already-in-use':
          console.log(`Email address ${this.state.email} already in use.`);
          break;
        case 'auth/invalid-email':
          console.log(`Email address ${this.state.email} is invalid.`);
          break;
        case 'auth/operation-not-allowed':
          console.log(`Error during sign up.`);
          break;
        case 'auth/weak-password':
          console.log('Password is not strong enough. Add additional characters including special characters and numbers.');
          break;
        default:
          console.log(error.message);
          break;
      }
  });

Hopefully that helps!


There's a simpler answer in the case of firebase admin sdk:

const uidExists = auth().getUser(uid).then(() => true).catch(() => false))
const emailExists = auth().getUserByEmail(email).then(() => true).catch(() => false))