本文介绍了Firebase token.email_verified变得很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在制作一个博客,要求用户通过Firebase登录.要发表评论,他们的电子邮件必须经过验证

Ok so im making a blog which requires users to login through firebase. To post comments, their email has to be verified

我知道如何验证电子邮件,我使用测试帐户进行了验证.当我在控制台上输入

I know how to verify the email, and i did so with my test account. When i typed into the console

firebase.auth().currentUser.emailVerified

它返回true,所以是的,我的电子邮件已经过验证.

it returned true, so yes my email was verified.

但是注释.validate规则要求对用户进行验证,就像这样:

But the comment .validate rule requires the user to be validated, like so:

auth.token.email_verified === true

但是它不起作用,所以我将其删除,然后又开始起作用

However it wasn't working, so i removed it and it began to work again

经过一番阅读,我意识到我必须

After a bit of reading, I realized that i had to

const credentials = firebase.auth.EmailAuthProvider.credential(
  user.email, password);

user.reauthenticateWithCredential(credentials)
  .then(() => { /* ... */ });

这使它完美地工作.原因是Firebase服务器似乎花了一些时间来更新其后端验证,但是重新认证立即强制更新.

And that makes it work perfectly. The explanation was it apparantly takes the firebase server some time to update its backend validation, but reauthenticating forces the update immediately.

但是,由于遇到以下问题,我对如何要求用户重新进行身份验证感到困惑

However, I am stumped on how to ask the user to reauthenticate themselves, as i have the following problem

我如何知道何时验证用户(firebase.auth().currentUser.emailValidated),同时不更新Firebase后端(auth.token.email_verified === true为false),以便我可以更新我的UI并提示用户重新进行身份验证

How do I know when the users is validated (firebase.auth().currentUser.emailValidated), and at the same time the firebase backend is not updated (auth.token.email_verified === true is false) so that i can update my UI and prompt the user to reauthenticate

基本上我怎么知道客户端上的auth.token.email_verified === true尚未更新

Basically how can i know when auth.token.email_verified === true is not updated yet on the client side

编辑,是否还有一种无需重新认证即可更新后端验证的客户端解决方案?

edit also is there a client side solution without reauthentication that updates the backend validation?

编辑,我尝试了user.reload().then(() => window.location.replace('/')),但没有成功

edit I tried user.reload().then(() => window.location.replace('/')) but it didnt work

推荐答案

这可能正在发生:

firebase.auth().currentUser.emailVerified.但是,auth.token.email_verified是从ID令牌获取其值的,直到令牌过期或您强制刷新,该ID令牌才会更新.因此,您可能必须调用firebase.auth().currentUser.getIdToken(true)强制刷新以更新发送到Firebase数据库后端的令牌声明.

firebase.auth().currentUser.emailVerified is updated when firebase.auth().currentUser.reload() is called after verification. However auth.token.email_verified gets its value from the ID token which will not get updated until it gets expired or you force refresh. So you may have to call firebase.auth().currentUser.getIdToken(true) to force refresh to update the token claim which is sent to the Firebase Database backend.

这篇关于Firebase token.email_verified变得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 11:22