nuxt-community / firebase-module

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

[Feature Request] Auth-Initialize: Optionally refresh expired idToken via onIdTokenChanged() instead of onAuthStatusChanged() #411

Closed Kasonz closed 3 years ago

Kasonz commented 3 years ago

Hi there,

Great module first of all!

We are looking for using nuxt-firebase to replace our custom logic + firebase package. I have read the document and played around.

Can you please help here?

let's say the app is configured as service worker + SSR: true

  1. We got the user idToken from nuxtServerInt or authstateChanged action, configured the token as Axios authorization header
  2. the user works on the page for more than 1 hour (the id token apparently is expired)
  3. user click button to send CURD request to our server middleware (or same-origin backend) via Axios (/api/**)

Is there a possibility service worker or nuxt-firebase to auto-detect & auto-refresh the id token in the request header? I only see it auto-inject in HTML page

Many thanks

lupas commented 3 years ago

Hey @Kasonz

Please excuse my late response - have you found a solution to your problem already?

I have setup a similar demo project that is pretty much doing the same as your project here: https://github.com/lupas/nuxt-firebase-server-auth-demo

You are right - onAuthStatusChanged is not triggered when the idToken expires. This is something Firebase changed with v4.0.0, when they introduced onIdTokenChanged() for handling this case.

See onidtokenchanged vs onauthstatechanged.

This module - so far - does not implement onIdTokenChanged() for you. So your solution would be to manually setup that listener and refresh the user/token when this action is triggered.

We intentionally do not automatically call onIdTokenChanged, because we expect most of our users to use other Firebase JS SDK services which do not rely on this action, because the SDK automatically refreshes the idToken in that cases if expired.

So solution for you at this point: Implement the onIdTokenChanged listener yourself.

Note/ToDo for myself:

Hope that helps, I keep this issue open as a Feature request.

BR, Pascal

lupas commented 3 years ago

@Kasonz Implemented this as a feature in v7.4.0, you can now define a store mutation or action that gets triggered when onIdTokenChanged() is triggered by Firebase. Using the options sets up the handler in the background.

Handling is exactly the same as with onAuthStateChanged - except that after one hour the function gets triggered.

Hope that helps!

br, Pascal

lupas commented 3 years ago

Docs are here: https://firebase.nuxtjs.org/service-options/auth#onidtokenchangedaction