问题描述
好的,我正在制作一个要求用户通过 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 是否还有客户端解决方案无需重新验证即可更新后端验证?
edit also is there a client side solution without reauthentication that updates the backend validation?
edit 我试过 user.reload().then(() => window.location.replace('/'))
但没用>
edit I tried user.reload().then(() => window.location.replace('/'))
but it didnt work
推荐答案
这是可能发生的事情:
firebase.auth().currentUser.emailVerified
在验证后调用 firebase.auth().currentUser.reload()
时更新.但是 auth.token.email_verified
从 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 变得很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!