[英]how to send verification email to users using Firebase Auth
I want to send verification email to users who just signed up.我想向刚刚注册的用户发送验证 email。 I have been looking for a solution for this and thought I found one, but it didn't work.我一直在为此寻找解决方案,并认为我找到了一个,但它没有用。 I checked out the Firebase Auth document and tried code on the page, but it also couldn't be the solution.我查看了 Firebase Auth 文档并在页面上尝试了代码,但它也不是解决方案。
My code is below The "try" actually runs when pressing the sign-up button and I can see it.我的代码在下面按下注册按钮时“尝试”实际上运行,我可以看到它。 In the Firebase Auth console page, I can see the user just created but didn't get a verification email in the inbox.在 Firebase Auth 控制台页面中,我可以看到用户刚刚创建但收件箱中没有获得验证 email。
const handleSignup = async (email, pswd) => {
try {
await createUserWithEmailAndPassword(auth, email, pswd);
setSignupEmail("");
setSignupPassword("");
setSignupPasswordAgain("");
signOut(auth);
sendEmailVerification(auth.currentUser);
alert("Check your email for a verification link.");
} catch (err) {
console.log(err);
}
};
You call sendEmailVerification()
after having called signOut()
so auth.currentUser
is null
when you pass it to sendEmailVerification()
.您在调用 signOut signOut()
sendEmailVerification()
,因此当您将auth.currentUser
传递给sendEmailVerification()
时,它是null
。 You should see an error logged in the catch block.您应该会在 catch 块中看到记录的错误。
Moreover, both sendEmailVerification()
and signOut()
methods are asynchronous and return Promises.此外, sendEmailVerification()
和signOut()
方法都是异步的并返回 Promises。
The following should do the trick (untested):以下应该可以解决问题(未经测试):
const handleSignup = async (email, pswd) => {
try {
await createUserWithEmailAndPassword(auth, email, pswd);
setSignupEmail("");
setSignupPassword("");
setSignupPasswordAgain("");
await sendEmailVerification(auth.currentUser);
await signOut(auth);
alert("Check your email for a verification link.");
} catch (err) {
console.log(err);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.