From e74c7ca855fd13ac6d88437380f67d484571d4e5 Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Fri, 29 Oct 2021 11:41:53 -0700 Subject: [PATCH 1/3] Fix double-onAuthStateChanged call bug --- packages/auth/src/core/auth/auth_impl.ts | 1 + packages/auth/src/platform_browser/auth.test.ts | 16 ++++++++++++++++ .../integration/webdriver/persistence.test.ts | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/auth/src/core/auth/auth_impl.ts b/packages/auth/src/core/auth/auth_impl.ts index af3258a8433..e34a845669e 100644 --- a/packages/auth/src/core/auth/auth_impl.ts +++ b/packages/auth/src/core/auth/auth_impl.ts @@ -139,6 +139,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService { } await this.initializeCurrentUser(popupRedirectResolver); + this.lastNotifiedUid = this.currentUser?.uid || null; if (this._deleted) { return; diff --git a/packages/auth/src/platform_browser/auth.test.ts b/packages/auth/src/platform_browser/auth.test.ts index 3b33f357db2..a8bbe4edf0c 100644 --- a/packages/auth/src/platform_browser/auth.test.ts +++ b/packages/auth/src/platform_browser/auth.test.ts @@ -319,6 +319,22 @@ describe('core/auth/initializeAuth', () => { }); }); + it('initialization sets the callback UID correctly', async () => { + const stub = sinon.stub( + _getInstance(inMemoryPersistence) + ); + stub._get.returns(Promise.resolve(testUser(oldAuth, 'uid').toJSON())); + let authStateChangeCalls = 0; + + const auth = await initAndWait(inMemoryPersistence) as AuthInternal; + auth.onAuthStateChanged(() => { + authStateChangeCalls++; + }); + + await auth._updateCurrentUser(testUser(auth, 'uid')); + expect(authStateChangeCalls).to.eq(1); + }); + context('#tryRedirectSignIn', () => { it('returns null and clears the redirect user in case of error', async () => { const stub = sinon.stub( diff --git a/packages/auth/test/integration/webdriver/persistence.test.ts b/packages/auth/test/integration/webdriver/persistence.test.ts index 3ec3f553281..bc76609a0fa 100644 --- a/packages/auth/test/integration/webdriver/persistence.test.ts +++ b/packages/auth/test/integration/webdriver/persistence.test.ts @@ -445,7 +445,7 @@ browserDescribe('WebDriver persistence test', (driver, browser) => { await driver.call(CoreFunction.SIGN_OUT); expect(await driver.getUserSnapshot()).to.be.null; await driver.selectMainWindow({ noWait: true }); - await driver.pause(500); + await driver.pause(700); expect(await driver.getUserSnapshot()).to.be.null; const cred2: UserCredential = await driver.call( From a8075fcc1bec7ae76ef3f2bb745fc4fd7c862be8 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 29 Oct 2021 11:43:36 -0700 Subject: [PATCH 2/3] Add changeset --- .changeset/thirty-rats-worry.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/thirty-rats-worry.md diff --git a/.changeset/thirty-rats-worry.md b/.changeset/thirty-rats-worry.md new file mode 100644 index 00000000000..818a2a28e3f --- /dev/null +++ b/.changeset/thirty-rats-worry.md @@ -0,0 +1,5 @@ +--- +"@firebase/auth": patch +--- + +Fix bug that caused onAuthStateChanged to be fired twice From 1c282326243a4ccf93d3a169521a9f98b8337bfa Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Fri, 29 Oct 2021 13:01:22 -0700 Subject: [PATCH 3/3] PR feedback --- packages/auth/src/platform_browser/auth.test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/auth/src/platform_browser/auth.test.ts b/packages/auth/src/platform_browser/auth.test.ts index a8bbe4edf0c..7d75c8bca12 100644 --- a/packages/auth/src/platform_browser/auth.test.ts +++ b/packages/auth/src/platform_browser/auth.test.ts @@ -332,6 +332,9 @@ describe('core/auth/initializeAuth', () => { }); await auth._updateCurrentUser(testUser(auth, 'uid')); + await new Promise(resolve => { + setTimeout(resolve, 200); + }); expect(authStateChangeCalls).to.eq(1); });