reactjs - 执行 createUserWithEmailAndPassword 后,React Native 使用 sendEmailVerification 检查用户电子邮件

标签 reactjs firebase react-native firebase-authentication react-native-firebase

我已经给代码分配了一项任务,即在用户在基于 react native 的注册屏幕中注册后向用户发送电子邮件验证。创建新用户是在一个名为 AuthProvider.js 的 js 文件中处理的。在 AuthContext.Provider 的返回值之一中,有一个处理创建新用户的操作,如下面的代码所示,并且工作正常。

    registerWithEmail: async (userDetails) => {
                        const { email, password } = userDetails;
    
                        return await auth()
                            .createUserWithEmailAndPassword(email, password)
                            .then(() => {
                       //wish to add on the send email verification action here
                                return true;
                            });
                    }

上面的返回 true 代码用于在注册屏幕中进行检查。如果我希望仅在向他们发送验证电子邮件并单击的条件下返回真实值。我该怎么做?我可以获得任何指导吗?

最佳答案

您可以通过这种方式将 async-await 语法与 `try-catch 结合使用。

registerWithEmail: async (userDetails) => {
  try {
    const { email, password } = userDetails;
    const {user} = await auth().createUserWithEmailAndPassword(email, password)
    await user.sendEmailVerification()
    return true
  } catch (e) {
    console.log(e)
    return false
  }

仅当电子邮件发送时才会返回 true。如果函数中有任何错误,它将触发 catch 并且函数将返回 false。

关于reactjs - 执行 createUserWithEmailAndPassword 后,React Native 使用 sendEmailVerification 检查用户电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69008874/

相关文章:

javascript - 注销时 Redux 不会返回到我的初始状态

javascript - Firebase .remove 不起作用;没有返回错误

javascript - 菱形 View react native

node.js - 当应用程序被终止时,如何跟踪我的移动应用程序用户

reactjs - 使用 Prop 值删除 useEffect Hook 中状态数组元素中的元素

javascript - 如何配置 sw-precache-webpack-plugin 来缓存自定义路径

firebase - 谷歌预定函数 : There was an error deploying functions?

swift - 如何在 Firebase 中检索 UID 和自动 ID 下的数据 Swift

javascript - 指定组件所需的 props

reactjs - 使用 React 缺少 MapBox CSS