简体   繁体   English

如何使用 Firebase Auth 向用户发送验证 email

[英]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.

相关问题 Flutter:如何使用邮箱验证FIrebase Auth - Flutter : How to use Email Verification FIrebase Auth 如何发送OTP到email email验证flutter firebase - how to send OTP to email for email verification in flutter firebase Firebase-auth/email-change-needs-verification “多因素用户必须始终拥有经过验证的电子邮件” - Firebase-auth/email-change-needs-verification "multifactor users must always have a verified email" Firebase Auth,email验证不创建用户? - Firebase Auth, email verification without creating a user? 对自定义电子邮件使用 firebase 电子邮件验证 - Using firebase email verification for a custom email 如何在 React Native 中向用户发送 Firebase 验证 email(以便用户可以验证他们的电子邮件)? - How do you send a Firebase verification email (so that a user can verify their email) to a user in React Native? 从发送验证邮件接收深度链接 (Flutter / Firebase) - Receive Deep Link from Send Verification Email (Flutter / Firebase) Firebase Email 验证无效 - Firebase Email Verification is not working NullPointerException in send email验证,Android studio with google firebase - NullPointerException in send email verification, Android studio with google firebase 使用 Firebase Auth 进行身份验证时请求短信验证码失败 - SMS verification code request failed when authenticating using Firebase Auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM