gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:21:54 25 4
gpt4 key购买 nike

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

下面是我将要使用的部分源代码。请让我知道如何纠正这个问题。

在我的控制台上出现以下错误:

TypeError: Cannot read property 'emailVerified' of 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();      }    });

最佳答案

我们能看到所有的代码吗?您确定您的 firebase.auth() 声明正确吗?

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

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

firebaseUser 应该有 emailVerified key 。像这样将 if else 语句移到 if firebaseUser 中:

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),说的是什么?

关于javascript - Firebase WEB - 未发送电子邮件验证。代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43155577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com