Skip to content

Commit 0765b5e

Browse files
authored
[Auth] Fix double-onAuthStateChanged call bug (#5686)
* Fix double-onAuthStateChanged call bug * Add changeset * PR feedback
1 parent 4594d3f commit 0765b5e

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

.changeset/thirty-rats-worry.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/auth": patch
3+
---
4+
5+
Fix bug that caused onAuthStateChanged to be fired twice

packages/auth/src/core/auth/auth_impl.ts

+1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
139139
}
140140

141141
await this.initializeCurrentUser(popupRedirectResolver);
142+
this.lastNotifiedUid = this.currentUser?.uid || null;
142143

143144
if (this._deleted) {
144145
return;

packages/auth/src/platform_browser/auth.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,25 @@ describe('core/auth/initializeAuth', () => {
319319
});
320320
});
321321

322+
it('initialization sets the callback UID correctly', async () => {
323+
const stub = sinon.stub(
324+
_getInstance<PersistenceInternal>(inMemoryPersistence)
325+
);
326+
stub._get.returns(Promise.resolve(testUser(oldAuth, 'uid').toJSON()));
327+
let authStateChangeCalls = 0;
328+
329+
const auth = await initAndWait(inMemoryPersistence) as AuthInternal;
330+
auth.onAuthStateChanged(() => {
331+
authStateChangeCalls++;
332+
});
333+
334+
await auth._updateCurrentUser(testUser(auth, 'uid'));
335+
await new Promise(resolve => {
336+
setTimeout(resolve, 200);
337+
});
338+
expect(authStateChangeCalls).to.eq(1);
339+
});
340+
322341
context('#tryRedirectSignIn', () => {
323342
it('returns null and clears the redirect user in case of error', async () => {
324343
const stub = sinon.stub(

packages/auth/test/integration/webdriver/persistence.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ browserDescribe('WebDriver persistence test', (driver, browser) => {
445445
await driver.call(CoreFunction.SIGN_OUT);
446446
expect(await driver.getUserSnapshot()).to.be.null;
447447
await driver.selectMainWindow({ noWait: true });
448-
await driver.pause(500);
448+
await driver.pause(700);
449449
expect(await driver.getUserSnapshot()).to.be.null;
450450

451451
const cred2: UserCredential = await driver.call(

0 commit comments

Comments
 (0)