ember-cloud-firestore-adapter icon indicating copy to clipboard operation
ember-cloud-firestore-adapter copied to clipboard

[WIP] Fix auth infinite loop with multi-tabs

Open mikkopaderes opened this issue 2 years ago • 2 comments

mikkopaderes avatar Nov 25 '22 04:11 mikkopaderes

Hi @mikkopaderes, you may already have spotted this issue but I was playing about with the "cherryPicker" idea today, using the latest commit on this PR.

  public async restore(): Promise<AuthenticatedData> {
    return new Promise((resolve, reject) => {
      const auth = getAuth();

      const unsubscribe = onAuthStateChanged(
        auth,
        async (user) => {
          unsubscribe();

          if (user) {
            resolve({ user: parseCherryPickedUser(user) });
          } else {
            getRedirectResult(auth)
              .then((credential) => {
                if (credential) {
                  resolve({ user: credential.user });
                } else {
                  reject();
                }
              })
              .catch(() => {
                reject();
              });
          }
        },
        () => {
          unsubscribe();
          reject();
        }
      );
    });
  }

I opened a session and logged in fine. I then opened two additional tabs on the same route. I logged out in one and all three sessions were invalidated. Then I attempted to login again but that was immediately invalidated by the others. I tried again and all three sessions fired up.

I logged everything out and discovered that on the initial attempt, onAuthStateChanged and getRedirectResult in the second and third sessions is returning null, so the promise is rejected.

roomman avatar Nov 28 '22 20:11 roomman

@roomman yeah, noticed that which is what's keeping this PR from being merged. There's some relevant info here so I'm trying to see if that would work for us.

mikkopaderes avatar Nov 29 '22 01:11 mikkopaderes