From 7af65eabb95982701e409005955d4b07b66d9eff Mon Sep 17 00:00:00 2001 From: Sam Olsen Date: Tue, 1 Jun 2021 16:16:11 -0700 Subject: [PATCH] Make anonymous sign in wait for auth to finish initializing --- .../auth-exp/src/core/strategies/anonymous.ts | 1 + .../test/integration/webdriver/anonymous.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/packages-exp/auth-exp/src/core/strategies/anonymous.ts b/packages-exp/auth-exp/src/core/strategies/anonymous.ts index 917d7dad105..97ddab32d6e 100644 --- a/packages-exp/auth-exp/src/core/strategies/anonymous.ts +++ b/packages-exp/auth-exp/src/core/strategies/anonymous.ts @@ -34,6 +34,7 @@ import { _castAuth } from '../auth/auth_impl'; */ export async function signInAnonymously(auth: Auth): Promise { const authInternal = _castAuth(auth); + await authInternal._initializationPromise; if (authInternal.currentUser?.isAnonymous) { // If an anonymous user is already signed in, no need to sign them in again. return new UserCredentialImpl({ diff --git a/packages-exp/auth-exp/test/integration/webdriver/anonymous.test.ts b/packages-exp/auth-exp/test/integration/webdriver/anonymous.test.ts index 2a9529997f6..8d30329dd43 100644 --- a/packages-exp/auth-exp/test/integration/webdriver/anonymous.test.ts +++ b/packages-exp/auth-exp/test/integration/webdriver/anonymous.test.ts @@ -52,4 +52,19 @@ browserDescribe('WebDriver anonymous auth test', driver => { ); expect(after.uid).to.eq(before.uid); }); + + it('user persists after refresh and sign in (no init wait)', async () => { + const { user: before }: UserCredential = await driver.call( + AnonFunction.SIGN_IN_ANONYMOUSLY + ); + await driver.webDriver.navigate().refresh(); + await driver.injectConfigAndInitAuth(); + + // At this point we aren't waiting for auth to "settle" + // Sign in before the first onAuthStateChanged has occurred + const { user: after }: UserCredential = await driver.call( + AnonFunction.SIGN_IN_ANONYMOUSLY + ); + expect(after.uid).to.eq(before.uid); + }); });