Unable to trger re-authentication with firebase v4, javascript

风流意气都作罢 提交于 2019-12-11 07:35:01

问题


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.


回答1:


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.
  }
});


来源:https://stackoverflow.com/questions/45039314/unable-to-trger-re-authentication-with-firebase-v4-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!