Skip to content

Commit

Permalink
[Auth] Fix double-onAuthStateChanged call bug (#5686)
Browse files Browse the repository at this point in the history
* Fix double-onAuthStateChanged call bug

* Add changeset

* PR feedback
  • Loading branch information
sam-gc authored Oct 29, 2021
1 parent 4594d3f commit 0765b5e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thirty-rats-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/auth": patch
---

Fix bug that caused onAuthStateChanged to be fired twice
1 change: 1 addition & 0 deletions packages/auth/src/core/auth/auth_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
}

await this.initializeCurrentUser(popupRedirectResolver);
this.lastNotifiedUid = this.currentUser?.uid || null;

if (this._deleted) {
return;
Expand Down
19 changes: 19 additions & 0 deletions packages/auth/src/platform_browser/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,25 @@ describe('core/auth/initializeAuth', () => {
});
});

it('initialization sets the callback UID correctly', async () => {
const stub = sinon.stub(
_getInstance<PersistenceInternal>(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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0765b5e

Please sign in to comment.