firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.86k stars 894 forks source link

[ADVICE] onIdTokenChanged explanation & usage #7803

Open michealroberts opened 1 year ago

michealroberts commented 1 year ago

Operating System

macOS v12.6.8

Browser Version

Version 119.0.6045.123 (Official Build) (arm64)

Firebase SDK Version

10.6.0

Firebase SDK Product:

Auth

Describe your project's tooling

TypeScript, Vite & Vue 3 Composition API

Describe the problem

I'm not convinced that onIdTokenChanged() is working either as documented, as the function name suggests, or as we think it should be. Apologies if it is the latter, and this is my misunderstanding of the function name.

When a user signs in, signs out, or when the browser is refreshed. The auth event is triggered and everything works as intended.

However, when the token expires after (correct me if I am wrong), one hour, the trigger is not met. Should the

Steps and code to reproduce issue

Essentially add the following subscription handler for an auth token "change", anywhere in your app:

onIdTokenChanged(auth, (authUser) => {
    console.log(...['%c[@observerly/sirius]', 'color: gray;'], 'onIdTokenChanged', authUser) <-- NOT TRIGGERING
    user.value = authUser
  })

However, when the user's token actually expires, it does not seem to trigger any sort of refresh or event that the name of this function suggests ...

renkelvin commented 1 year ago

Thanks @michealroberts for reporting the advice. As you can find here, this works as intended. The token itself remains unchanged while it becomes expired because of the time change.