firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

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

Open maximedupre opened this issue 3 years ago • 1 comments

[REQUIRED] Describe your environment

  • Operating System version: macOS 12.5.1
  • Browser version: Brave 1.43.93
  • Firebase SDK version: 9.9.4
  • Firebase Product: auth

[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.

maximedupre avatar Sep 16 '22 16:09 maximedupre

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)

sam-gc avatar Sep 19 '22 18:09 sam-gc