diff --git a/packages-exp/auth-exp/src/core/auth/initialize.test.ts b/packages-exp/auth-exp/src/core/auth/initialize.test.ts index 63a9864b187..7226d5b5374 100644 --- a/packages-exp/auth-exp/src/core/auth/initialize.test.ts +++ b/packages-exp/auth-exp/src/core/auth/initialize.test.ts @@ -50,6 +50,7 @@ import { import { ClientPlatform, _getClientVersion } from '../util/version'; import { initializeAuth } from './initialize'; import { registerAuth } from './register'; +import { debugErrorMap, prodErrorMap } from '../errors'; describe('core/auth/initialize', () => { let fakeApp: FirebaseApp; @@ -127,7 +128,8 @@ describe('core/auth/initialize', () => { } } - const fakePopupRedirectResolver: PopupRedirectResolver = FakePopupRedirectResolver; + const fakePopupRedirectResolver: PopupRedirectResolver = + FakePopupRedirectResolver; before(() => { registerAuth(ClientPlatform.BROWSER); @@ -203,15 +205,63 @@ describe('core/auth/initialize', () => { const auth = initializeAuth(fakeApp, { popupRedirectResolver: fakePopupRedirectResolver }) as AuthInternal; - await ((auth as unknown) as _FirebaseService)._delete(); + await (auth as unknown as _FirebaseService)._delete(); await auth._initializationPromise; expect(auth._isInitialized).to.be.false; }); - it('should throw if called more than once', () => { - initializeAuth(fakeApp); - expect(() => initializeAuth(fakeApp)).to.throw(); + it('should not throw if called again with same (no) params', () => { + const auth = initializeAuth(fakeApp); + expect(initializeAuth(fakeApp)).to.equal(auth); + }); + + it('should not throw if called again with same params', () => { + const auth = initializeAuth(fakeApp, { + errorMap: prodErrorMap, + persistence: fakeSessionPersistence, + popupRedirectResolver: fakePopupRedirectResolver + }); + expect( + initializeAuth(fakeApp, { + errorMap: prodErrorMap, + persistence: fakeSessionPersistence, + popupRedirectResolver: fakePopupRedirectResolver + }) + ).to.equal(auth); + }); + + it('should throw if called again with different params (popupRedirectResolver)', () => { + initializeAuth(fakeApp, { + popupRedirectResolver: fakePopupRedirectResolver + }); + expect(() => + initializeAuth(fakeApp, { + popupRedirectResolver: undefined + }) + ).to.throw(); + }); + + it('should throw if called again with different params (errorMap)', () => { + initializeAuth(fakeApp, { + errorMap: prodErrorMap + }); + expect(() => + initializeAuth(fakeApp, { + errorMap: debugErrorMap + }) + ).to.throw(); + }); + + it('should throw if called again with different params (persistence)', () => { + initializeAuth(fakeApp, { + persistence: [inMemoryPersistence, fakeSessionPersistence] + }); + expect(() => + initializeAuth(fakeApp, { + persistence: [fakeSessionPersistence, inMemoryPersistence] + }) + ).to.throw(); }); }); }); diff --git a/packages-exp/auth-exp/src/core/auth/initialize.ts b/packages-exp/auth-exp/src/core/auth/initialize.ts index 32e254ad84a..8f8528969eb 100644 --- a/packages-exp/auth-exp/src/core/auth/initialize.ts +++ b/packages-exp/auth-exp/src/core/auth/initialize.ts @@ -16,6 +16,7 @@ */ import { _getProvider, FirebaseApp } from '@firebase/app-exp'; +import { deepEqual } from '@firebase/util'; import { Auth, Dependencies } from '../../model/public_types'; import { AuthErrorCode } from '../errors'; @@ -54,7 +55,12 @@ export function initializeAuth(app: FirebaseApp, deps?: Dependencies): Auth { if (provider.isInitialized()) { const auth = provider.getImmediate() as AuthImpl; - _fail(auth, AuthErrorCode.ALREADY_INITIALIZED); + const initialOptions = provider.getOptions() as Dependencies; + if (deepEqual(initialOptions, deps ?? {})) { + return auth; + } else { + _fail(auth, AuthErrorCode.ALREADY_INITIALIZED); + } } const auth = provider.initialize({ options: deps }) as AuthImpl; @@ -67,9 +73,8 @@ export function _initializeAuthInstance( deps?: Dependencies ): void { const persistence = deps?.persistence || []; - const hierarchy = (Array.isArray(persistence) - ? persistence - : [persistence] + const hierarchy = ( + Array.isArray(persistence) ? persistence : [persistence] ).map(_getInstance); if (deps?.errorMap) { auth._updateErrorMap(deps.errorMap); diff --git a/packages-exp/auth-exp/src/core/errors.ts b/packages-exp/auth-exp/src/core/errors.ts index e955c4c1c0f..a79ecca7292 100644 --- a/packages-exp/auth-exp/src/core/errors.ts +++ b/packages-exp/auth-exp/src/core/errors.ts @@ -349,7 +349,10 @@ function _debugErrorMap(): ErrorMap { [AuthErrorCode.WEB_STORAGE_UNSUPPORTED]: 'This browser is not supported or 3rd party cookies and data may be disabled.', [AuthErrorCode.ALREADY_INITIALIZED]: - 'Auth can only be initialized once per app.' + 'initializeAuth() has already been called with ' + + 'different options. To avoid this error, call initializeAuth() with the ' + + 'same options as when it was originally called, or call getAuth() to return the' + + ' already initialized instance.' }; }