Skip to content

Commit 28a63eb

Browse files
committed
Make initializeAuth() idempotent
1 parent 3871f26 commit 28a63eb

File tree

2 files changed

+58
-8
lines changed

2 files changed

+58
-8
lines changed

packages-exp/auth-exp/src/core/auth/initialize.test.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
import { isNode } from '@firebase/util';
3232

3333
import { expect } from 'chai';
34-
import { inMemoryPersistence } from '../../../internal';
34+
import { browserLocalPersistence, browserSessionPersistence, inMemoryPersistence } from '../../../internal';
3535

3636
import { AuthInternal } from '../../model/auth';
3737
import {
@@ -50,6 +50,7 @@ import {
5050
import { ClientPlatform, _getClientVersion } from '../util/version';
5151
import { initializeAuth } from './initialize';
5252
import { registerAuth } from './register';
53+
import { debugErrorMap, prodErrorMap } from '../errors';
5354

5455
describe('core/auth/initialize', () => {
5556
let fakeApp: FirebaseApp;
@@ -209,9 +210,49 @@ describe('core/auth/initialize', () => {
209210
expect(auth._isInitialized).to.be.false;
210211
});
211212

212-
it('should throw if called more than once', () => {
213-
initializeAuth(fakeApp);
214-
expect(() => initializeAuth(fakeApp)).to.throw();
213+
it('should not throw if called again with same (no) params', () => {
214+
const auth = initializeAuth(fakeApp);
215+
expect(initializeAuth(fakeApp)).to.equal(auth);
216+
});
217+
218+
it('should not throw if called again with same params', () => {
219+
const auth = initializeAuth(fakeApp, {
220+
errorMap: prodErrorMap,
221+
persistence: browserSessionPersistence,
222+
popupRedirectResolver: fakePopupRedirectResolver
223+
});
224+
expect(initializeAuth(fakeApp, {
225+
errorMap: prodErrorMap,
226+
persistence: browserSessionPersistence,
227+
popupRedirectResolver: fakePopupRedirectResolver
228+
})).to.equal(auth);
229+
});
230+
231+
it('should throw if called again with different params (popupRedirectResolver)', () => {
232+
initializeAuth(fakeApp, {
233+
popupRedirectResolver: fakePopupRedirectResolver
234+
});
235+
expect(() => initializeAuth(fakeApp, {
236+
popupRedirectResolver: undefined
237+
})).to.throw();
238+
});
239+
240+
it('should throw if called again with different params (errorMap)', () => {
241+
initializeAuth(fakeApp, {
242+
errorMap: prodErrorMap
243+
});
244+
expect(() => initializeAuth(fakeApp, {
245+
errorMap: debugErrorMap
246+
})).to.throw();
247+
});
248+
249+
it('should throw if called again with different params (persistence)', () => {
250+
initializeAuth(fakeApp, {
251+
persistence: [browserLocalPersistence, browserSessionPersistence]
252+
});
253+
expect(() => initializeAuth(fakeApp, {
254+
persistence: [browserSessionPersistence, browserLocalPersistence]
255+
})).to.throw();
215256
});
216257
});
217258
});

packages-exp/auth-exp/src/core/auth/initialize.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import { _getProvider, FirebaseApp } from '@firebase/app-exp';
19+
import { deepEqual } from '@firebase/util';
1920
import { Auth, Dependencies } from '../../model/public_types';
2021

2122
import { AuthErrorCode } from '../errors';
@@ -54,7 +55,16 @@ export function initializeAuth(app: FirebaseApp, deps?: Dependencies): Auth {
5455

5556
if (provider.isInitialized()) {
5657
const auth = provider.getImmediate() as AuthImpl;
57-
_fail(auth, AuthErrorCode.ALREADY_INITIALIZED);
58+
const initialOptions = provider.getOptions() as Dependencies;
59+
if (
60+
initialOptions.errorMap === deps?.errorMap &&
61+
initialOptions.popupRedirectResolver === deps?.popupRedirectResolver &&
62+
deepEqual(initialOptions.persistence as object, deps?.persistence as object)
63+
) {
64+
return auth;
65+
} else {
66+
_fail(auth, AuthErrorCode.ALREADY_INITIALIZED);
67+
}
5868
}
5969

6070
const auth = provider.initialize({ options: deps }) as AuthImpl;
@@ -67,9 +77,8 @@ export function _initializeAuthInstance(
6777
deps?: Dependencies
6878
): void {
6979
const persistence = deps?.persistence || [];
70-
const hierarchy = (Array.isArray(persistence)
71-
? persistence
72-
: [persistence]
80+
const hierarchy = (
81+
Array.isArray(persistence) ? persistence : [persistence]
7382
).map<PersistenceInternal>(_getInstance);
7483
if (deps?.errorMap) {
7584
auth._updateErrorMap(deps.errorMap);

0 commit comments

Comments
 (0)