繁体   English   中英

Firebase auth sendEmailVerification 不发送 email // 反应

[英]Firebase auth sendEmailVerification not sending email // react

我有一个使用 firebase auth 的 react 应用程序的注册表单。

注册 function 效果很好,只是没有发送验证 email。 这是我正在使用的代码:

  const registerUser = async (email, name, password) => {
    try {
      console.log("> Registering user")
      setLoading(true);
      const {
        user
      } = await createUserWithEmailAndPassword(auth, email, password)
  
      console.log("> Updating profile")
      await updateProfile(user, {
        displayName: name,
      })
      .then(()=>{
        // send verification mail.
        sendEmailVerification(auth.currentUser.email);
      auth.signOut();
      alert("Email sent");
    })
    .catch(alert); 
  
     window.location.pathname = `/subscriptions/${user.uid}`;
    } catch (e) {
      console.log(e)
    }
  
    setLoading(false)
  };

警报(“已发送电子邮件”)工作正常,过去已发送验证 email。 然而,从那以后我改变了它,不记得我用什么让它发送验证 email。

避免将传统的.then().catch()语法与现代async / await语法混合,因为这可能会导致混淆。

在您的代码中,您无需等待它们完成即可调用sendEmailVerification()auth.signOut() 这意味着立即调用alert("Email sent") ,然后调用window.location.pathname = `/subscriptions/${user.uid}` ,取消这两个操作。

const registerUser = async (email, name, password) => {
  try {
    console.log("> Registering user")
    setLoading(true);
    const { user } = await createUserWithEmailAndPassword(auth, email, password)
  
    console.log("> Updating profile")
    await updateProfile(user, { displayName: name });

    await sendEmailVerification(user.email);

    await auth.signOut();

    alert("Email sent"); // avoid using alert() as it blocks the thread
  
    window.location.pathname = `/subscriptions/${user.uid}`;
  } catch (e) {
    // consider swapping below line to `setErrorMessage()` or similar
    // and display the message above your form
    alert('Failed to send email verification: ' + (e.message || e)); 
    console.error('Failed to send email verification: ', e);
  }
  
  setLoading(false)
};

如果您想在每个步骤之后使用自定义错误消息,请在此处查看此答案以了解如何使用catch()来包装错误(但仅使用new Error()作为new functions.https.HttpsError()用于服务器端代码)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM