Firebase WEB - 未发送电子邮件验证。代码有什么问题

时间:2017-04-01 10:17:54

标签: javascript authentication firebase firebase-authentication

好吧,我尝试过多种方法来触发函数sendEmailVerification()。但没有一个成功。文档也没有帮助。

以下是我将使用的源代码的一部分。请告诉我如何纠正这个问题。

在我的控制台上,我收到以下错误:

  

TypeError:无法读取属性' emailRerified'为null            at Object.firebase.auth.onAuthStateChanged.firebaseUser [as next]


        btnSignUpWithGoogle.addEventListener('click', e => {
            var provider = new firebase.auth.GoogleAuthProvider();

            firebase.auth().signInWithPopup(provider).then(function(result) {
            var token = result.credential.accessToken;
            var user = result.user;
          }).catch(function(error) {
            var errorCode = error.code;
            var errorMessage = error.message;
            var email = error.email;
            var credential = error.credential;
            console.log(errorCode);
          });
    });

    btnLogin.addEventListener('click', e => {
      const email = txtEmail.value;
      const pass = txtPassword.value;
      const auth = firebase.auth();

      const promise = auth.signInWithEmailAndPassword(email, pass);
      promise.catch(e => console.log(e.message));

      txtEmail.value = "";
      txtPassword.value = "";
    });

    btnSignUp.addEventListener('click', e => {
      const email = txtEmail.value;
      const pass = txtPassword.value;
      const auth = firebase.auth();

      const promise = auth.createUserWithEmailAndPassword(email, pass);
      promise.catch(e => console.log(e.message));

      txtEmail.value = "";
      txtPassword.value = "";

      const emailVerified = firebaseUser.emailVerified;

      if (!emailVerified){
        firebase.auth().firebaseUser.sendEmailVerification().then(function(){
          alert('Please check your email to verify your Account.');
        });
      } else {
        alert('Your Email has been verified!');
      }

    });

    firebase.auth().onAuthStateChanged(firebaseUser => {
      if (firebaseUser) {
        console.log(firebaseUser);
        console.log('Logged IN!');
        btnLogout.style.visibility = 'visible';
    }
      if (firebaseUser.emailVerified) {
        console.log('Email is verified');
      }
      else {
        console.log('Email is not verified');
        firebaseUser.sendEmailVerification();
      }

    });

2 个答案:

答案 0 :(得分:1)

我们可以看到所有代码吗?你确定你的firebase.auth()被正确声明了吗?

您的身份验证应如下所示:

const firebaseApp = firebase.initializeApp(firebaseConfig, 'Client');
const firebaseAuth = firebaseApp.auth();

firebaseUser应该有emailVerified密钥。如果firebaseUser像这样移动你的if else语句:

firebase.auth().onAuthStateChanged(firebaseUser => {
  if (firebaseUser) {
    console.log(firebaseUser);
    console.log('Logged IN!');
    btnLogout.style.visibility = 'visible';

    if (firebaseUser.emailVerified) {
      console.log('Email is verified');
    }
    else {
      console.log('Email is not verified');
      firebaseUser.sendEmailVerification();
    }

  }

});

console.log(firebaseUser),说什么?

答案 1 :(得分:0)

sendEmailVerification()仅在您已将电子邮件地址加载到firebase中后才能生效。

检查以下代码: https://github.com/aqeelsmith/AngularJsData/blob/master/builds/angulardata/js/services/authentication.js - 第82行