我已经给代码分配了一项任务,即在用户在基于 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/