firebase / firebase-js-sdk

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

FR: There is no way to know quickly if firebase is processing a login redirect #6607

Open maximedupre opened 2 years ago

maximedupre commented 2 years ago

[REQUIRED] Describe your environment

[REQUIRED] Describe the problem

Firebase does not expose a functionality that allows us to know right away if the user is coming from a login redirect or not (using signInWithRedirect).

There is of course getRedirectResult which can tell us that, but only after we have waited that the promise resolves, which can take x seconds.

This prevents us from correctly showing a progress bar/loading indicator.

What we would need is something like isSignInRedirected: boolean. Then it would be possible to do something like this:

if(auth.isSignInRedirected) {
    this.showLoadingBar()

    const result = await getRedirectResult(auth) // can take a couple of seconds

    this.hideLoadingBar()

    // do something with results
}

Right now, I have found a non-official solution, but like all non-official solutions, it is very brittle as it may change at any moment. Firebase stores this localstorage key for a brief moment at redirect: "firebase:pendingRedirect:{someId}:[DEFAULT]". By checking if this key is available at application load, you can know right away if there is a pending redirect.

Perhaps Firebase could expose a method that checks on this key for us.

This is related to https://github.com/firebase/firebase-js-sdk/issues/595, but unfortunately, it has been locked and closed


Edit

I have looked all all the events onAuthStateChanged, onIdTokenChanged, beforeAuthStateChanged, but they take just as much time to report something as getRedirectResult.

sam-gc commented 2 years ago

Hi @maximedupre, thanks for filing this feature request. We will keep this issue open to gauge interest from the community, but since exposing the redirect state is for a relatively narrow use case, I'd suggest doing this manually (i.e. setting your own flag in session storage before calling signInWithRedirect()).

As an alternative, you can use the Firebase UI to handle your sign in page, and this has a built-in progress bar that does the same thing: https://github.com/firebase/firebaseui-web (Note that Firebase UI web requires the compatibility layer builds of the SDK)