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

uinbv5nw  于 12个月前  发布在  React
关注(0)|答案(1)|浏览(117)

我与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)

irtuqstp

irtuqstp1#

从下面的例子中注意到,就像你在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.' }); 
    }
  });

}

相关问题