无法使用firebase v4重新验证身份验证,javascript [英] Unable to trger re-authentication with firebase v4, javascript

查看:260
本文介绍了无法使用firebase v4重新验证身份验证,javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我们可以触发 onAuthStateChanged ,而无需在使用这些方法验证电子邮件后对用户进行签名。

  firebase.auth.currentUser.reload()
firebase.auth.currentUser.getToken(true)

我将它们更新为 getIdToken

  firebase.auth.currentUser.reload()
firebase.auth.currentUser.getIdToken(true)

然而,firebase的v4以前和新的方法不会重新触发 onAuthStateChange 了。现在还有办法实现吗?即注册流程,用户不需要注销 - >注册 - >验证电子邮件 - >点击继续按钮,调用上面的两个函数 - > onAuthStateChanged 现在验证用户的电子邮件触发get。

解决方案

在版本2.x的Firebase身份验证中,当用户的认证状态改变时被调用。所以,当他们登录或退出。



在Firebase身份验证SDK的3.x版本中,同样的回调也被调用来进行令牌刷新。但是这会导致开发人员在更新UI时遇到问题,因为即使认证状态没有改变(即使令牌没有改变),也会调用这个回调函数。



这就是为什么这个行为在v4中的确已经发生了变化: onAuthStateChanged 不再为标记刷新触发。在这种情况下,有一个单独的 onIdTokenChanged()方法可以触发 https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onIdTokenChanged

从那里:


添加一个观察者来更改登录用户的ID令牌,其中包括登录,注销和令牌刷新事件。此方法与 firebase.auth.Auth#onAuthStateChanged 在4.0.0之前具有相同的行为。



示例:

$ $ p $ firebase.auth()。onIdTokenChanged(function(user){
if(user){
//用户登录或令牌刷新。
}
});



I was previously able to trigger onAuthStateChanged without needing to sign user out after they verified their email using these methods called one after another.

firebase.auth.currentUser.reload()
firebase.auth.currentUser.getToken(true)

I updated them to getIdToken

firebase.auth.currentUser.reload()
firebase.auth.currentUser.getIdToken(true)

However with v4 of firebase previous and new approach are not re-triggering onAuthStateChange anymore. Is there a way to still achieve it now? i.e. for sign up flow where user doesn't need to log out -> signs up -> verifies email -> clicks "Continue" button that calls 2 functions above -> onAuthStateChanged get's triggered with user email now being verified.

解决方案

In version 2.x of Firebase Authentication, the auth state changed handler was only invoked when the user's authentication state changed. So when they signed in or out.

In version 3.x of the Firebase Authentication SDK, the same callback was also invoked for token refreshes. But this lead to developers having problems efficiently updating their UI, since this callback would be invoked even when the authentication state didn't change (even though the token did).

That's why this behavior has indeed changed in v4: the onAuthStateChanged doesn't fire anymore for token refreshes. There is a separate onIdTokenChanged() method that does fire in that case: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onIdTokenChanged

From there:

Adds an observer for changes to the signed-in user's ID token, which includes sign-in, sign-out, and token refresh events. This method has the same behavior as firebase.auth.Auth#onAuthStateChanged had prior to 4.0.0.

Example:

firebase.auth().onIdTokenChanged(function(user) {
  if (user) {
    // User is signed in or token was refreshed.
  }
});

这篇关于无法使用firebase v4重新验证身份验证,javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆