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 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..7d75c8bca12 100644 --- a/packages/auth/src/platform_browser/auth.test.ts +++ b/packages/auth/src/platform_browser/auth.test.ts @@ -319,6 +319,25 @@ 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')); + await new Promise(resolve => { + setTimeout(resolve, 200); + }); + 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(