Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Auth] Fix double-onAuthStateChanged call bug #5686

Merged
merged 4 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
sam-gc marked this conversation as resolved.
Show resolved Hide resolved
});

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