Failed to re-authenticate with firebase v4, javascript

I was previously able to run onAuthStateChanged

without having to sign a user after checking their email using these methods called one by one.

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

      

I have updated them to getIdToken

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

      

However, with v4 the previous and new approaches firebase no longer starts onAuthStateChange

. Is there a way to still achieve this now? those. to register a stream when the user does not need to log out → subscribes → checks the email → clicks the "Continue" button, which calls the 2 functions above → onAuthStateChanged

which are launched with the verification of the user's email.

+1


source to share


1 answer


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

In version 3.x of the Firebase SDK, the same callback was also called to call tokens. But this leads developers to have trouble updating their UI efficiently, as this callback will be called even if the authentication state has not changed (although the token did).

That's why this behavior really changed in v4: onAuthStateChanged

no longer triggered to update tokens. There is a separate method onIdTokenChanged()

that fires in this case: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#onIdTokenChanged



From there:

Adds a watcher to changes in login tokens with a user ID that includes login events, logouts, and tokens. This method has the same behavior as it firebase.auth.Auth#onAuthStateChanged

had before 4.0.0.

Example:

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

      

+2


source







All Articles