我正在开发一个具有现有用户群的网络应用程序。电子邮件验证最初并未在登录流程中实现。
我已经成功添加了为所有新注册发送验证电子邮件的代码,但我还想制作一个小页面(or modal)其中将向当前用户显示一个按钮,该按钮会将验证链接发送到他们的收件箱
我创建用户的当前注册流程createUserWithEmailAndPassword https://firebase.google.com/docs/auth/web/password-auth#create_a_password-based_account我能够访问user.user.sendEmailVerification
方法这样做,但找不到任何方法来访问此方法来为现有用户实现该功能。
有没有办法访问sendEmailVerification https://firebase.google.com/docs/auth/web/manage-users#send_a_user_a_verification_email创建用户后的方法?
我假设它将在onAuthStateChange https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user触发但实施会导致糟糕的用户体验(因为我不想在用户每次登录时提示他们)
Edit:
我知道文档指出我们可以使用firebase.auth().currentUser
获取当前用户,但是,由于某种原因没有起作用。
另外,我发现参考 https://stackoverflow.com/questions/37883981/cant-get-currentuser-on-load网上建议不再使用该方法,他们提到使用onAuthStateChange
方法,这就是我研究这种方法的原因
你可以尝试这个方法:
const btnVerifyEmail = document.getElementById("btn-verify-id")
btnVerifyEmail.onclick = function () {
const user = firebase.auth().currentUser;
user.sendEmailVerification().then(function() {
// Email sent.
console.log("Email Sent")
}).catch(function(error) {
// An error happened.
console.log(error)
});
}
文档中正确提到了这一点here https://firebase.google.com/docs/auth/web/manage-users#send_a_user_a_verification_email
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)