How to find out if an email is already registered with Firebase Simple Login?

前端 未结 4 1559
广开言路
广开言路 2020-12-30 06:10

I am using firebase (with Angularfire) for an html5 phone app. The user inputs only their email in the start screen, and then, depending on whether that email is already reg

4条回答
  •  长情又很酷
    2020-12-30 06:36

    A little easier example to understand how to catch if the user already exists when signing up with email / password

    1. Catch the error
    2. If "auth/email-already-in-use" exist
    3. Call another function that contains .signInWithEmailAndPassword()

    Example

        .createUserWithEmailAndPassword(email, password)
    
        .catch(error => {
          // Handle Errors here.
          const errorCode = error.code;
          const errorMessage = error.message;
    
          // Catch this errorCode to know if user exists
          if (errorCode === 'auth/email-already-in-use') {
            // Call function X to sign user in instead
            signInMail(email, password);
            return;
          } 
    
          // Weak password?
          else if (errorCode == 'auth/weak-password') {
            console.log('The password is too weak.');
          } 
    
         else {
            console.log(errorMessage);
          }
          console.log(error);
        })
    

    Important part that you want to use in your own code would be this

    .catch(error => {
          // Handle Errors here.
          const errorCode = error.code;
          const errorMessage = error.message;
          if (errorCode === 'auth/email-already-in-use') {
            console.log('This email is already in use');
            // Your action
            return;
          }
        })
    

提交回复
热议问题