nuxt-community / firebase-module

🔥 Easily integrate Firebase into your Nuxt project. 🔥
https://firebase.nuxtjs.org
MIT License
641 stars 99 forks source link

How to Refresh Claims / ID token with onAuthStateChangedAction? #521

Closed sparkonium closed 3 years ago

sparkonium commented 3 years ago

When there are changes to user custom claims, tokens must be refreshed as per: https://firebase.google.com/docs/auth/admin/custom-claims#propagate_custom_claims_to_the_client

Propagate custom claims to the client

After new claims are modified on a user via the Admin SDK, they are propagated to an authenticated user on the client side via the ID token in the following ways:

  • A user signs in or re-authenticates after the custom claims are modified. The ID token issued as a result will contain the latest claims.
  • An existing user session gets its ID token refreshed after an older token expires.
  • An ID token is force refreshed by calling currentUser.getIdToken(true).

How could we manually call the action onAuthStateChanged on mount or from a button on a page to refresh the ID Token? We tried passing this.$fire.auth.currentUser but it did not work.

We would like to rewrite another Action/Store logic and to reuse if possible.

tibs245 commented 3 years ago

I don't understand : You want refresh token when you click on a button ? If yes :

const actions = {
  refreshToken() {
    this.$fire.auth.currentUser.getIdToken(true)
  },
}

And call action when you click on button

I hope to help you

tibs245 commented 3 years ago

I have see the context on precedent issue, Personally, when I execute getIdToken. It's when I execute firebase function and I know the change applied on claims

Not event launch onAuthStateChanged (who set claims with set mutation)

I actualize my userClaims data in store/state with update mutation

Conclusion: I use 2 different mutations for set and update my user claims.

And I call manually the update claims mutation

I hope to help you,

If you find better solution for update it automatically, I am interesting :)

lupas commented 3 years ago

Hey @tibs245

As far as I know this is the only way to do it... but could be wrong.

Closing for now.

br, Pascal