如何检查Firebase中的电子邮件是否在React Native中得到验证?

时间:2018-04-15 12:22:46

标签: javascript reactjs firebase react-native firebase-authentication

我使用firebase的技能是基本的,我想要的是,只有拥有经过验证的邮件的用户才能进入应用程序,如果没有显示未经验证的电子邮件的错误。在这里我的代码:

login (){

    const user = firebase.auth().currentUser;
    const emailVerified = user.emailVerified;

    const validate = this.refs.formId.getValue();
    if (validate && emailVerified != 'false') {
        firebase.auth().signInWithEmailAndPassword(validate.email, validate.password)
        .then(() => {

        })
        .catch((error) => {
            const errorCode = error.code;
            const errorMessage = error.message;
            if (errorCode === 'auth/wrong-password') {

            Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' })

            if (emailVerified === 'false') {

            Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' })

            }else{
            Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'Try Again' })
            }

        });
    }

}

我收到此错误: null不是对象(评估'user.emailVerified)

1 个答案:

答案 0 :(得分:0)

通过示例下的通知,就像您在documentation中使用firebase.auth().currentUser一样:

  

注意:currentUser也可能为null,因为auth对象没有   完成初始化。如果你使用观察者来跟踪   用户的登录状态,您无需处理此案例。

@bdroid,您不需要通过改变此登录过程的流程来集成它们。我认为这也提供了正确的登录流程,首先调用signInWithEmailAndPassword,在检测到用户是否已经过验证后,决定在完成授权登录和未完成授权时单独做什么:

login (){

  const validate = this.refs.formId.getValue();
  firebase.auth().signInWithEmailAndPassword(validate.email, validate.password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    if (errorCode === 'auth/wrong-password') {
      Toast.show({ text: 'Wrong password!', position: 'bottom', buttonText: 'Try Again' });
    }

  });

  firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
      if (user.emailVerified === false) {
        Toast.show({ text: 'Email Not Verified!', position: 'bottom', buttonText: 'Try Again' });
      } else {

        // successful login 

      }
    } else {
      //  Toast.show({ text: 'Something Wrong!', position: 'bottom', buttonText: 'No user is signed in.' }); 
    }
  });

}