new*_*der 9 firebase react-native firebase-authentication
编辑-这个问题仍然没有答案。有一个想法可以听onIdTokenChanged
,但是令牌每小时刷新一次,这对我来说不是实际的解决方案。我在这里发布了后续问题,如果人们可以帮助我的话,我会得到帮助,因为我已经坐了一个星期了这个问题。
我正在编写一个简单的反应本机应用程序,并且我想仅在用户验证其电子邮件后才显示我的主页。据我了解,没有侦听器可以用来侦听用户已验证其电子邮件的事件。我正在使用firebase.auth().onAuthStateChanged((user) => {....})
,但侦听器onAuthStateChanged
是在用户登录或注册后调用的,而不是在用户验证其电子邮件后调用的。建议使用很少的地方,firebase.auth().user.reload()
以便它将重新加载当前用户并从数据库中获取验证状态。但是,我不认为这是一个解决方案,因为我不知道何时应该重新加载当前用户,即我不知道何时单击验证链接。所以这个问题的可能解决方案是:
onAuthStateChanged
react-native中当前的实现,这样我就可以在覆盖时将其视为“灵感”吗?或者如果有人以前做过类似的事情,你能给我举个例子吗?firebase.auth().user.reload()
有些人建议在应用程序关闭并重新打开时使用。这是基于这样的假设:当向用户发送链接时,为了让他们单击该链接,他们需要关闭应用程序,然后再次重新打开它。我认为这是一个非常强有力的假设,考虑到他们可能通过笔记本电脑验证他们的电子邮件并且永远不会关闭应用程序,所以我认为这也不是一个好的解决方案。显然,这似乎是一个众所周知的问题,但没有太多好的解决方案。我认为最好的解决方案是向用户发送 6 位验证码,确认该代码后,我将重新加载当前用户,选择该字段emailVerified
,它将设置为 true,然后我将显示主屏幕。但是,有人可以帮助我如何在 React Native 和 Firebase 中发送自定义电子邮件吗?
如果有人有任何其他建议,请告诉我!
您只需在 actionCodeSettings 中传递一个 continue url 即可完成此操作,如下所示:
const res = await firebase.auth().createUserWithEmailAndPassword(
email,
password
);
await res.user.sendEmailVerification({
url: "https://yoursite.com/continue-url"
});
Run Code Online (Sandbox Code Playgroud)
sendEmailVerification()
因此,在我的项目中,我将和结合起来reload()
。
尝试一下:
await firebase
.auth()
.currentUser.sendEmailVerification(actionCodeSettings)
.then(() => {
//useState used on my loading (user can cancel this loading and exit this task
setTextContent('Waiting for verification. Check your email!\nYou can close this verification and came back later');
const unsubscribeOnUserChanged = firebase
.auth()
.onUserChanged(response => {
const unsubscribeSetInterval = setInterval(() => {//this works as a next in for-like
firebase.auth().currentUser.reload();
}, 30000);
if (response.emailVerified) {
clearInterval(unsubscribeSetInterval); //stop setInterval
setLoading(false); //close loading describes above
navigation.goBack(); //return to parent (in my case to profile)
return unsubscribeOnUserChanged(); //unsubscribe onUserChanged
}
});
})
.catch(error => {
setLoading(false);
setError(true);
errorHandle(error);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8925 次 |
最近记录: |