Skip to content

Make initializeAuth() idempotent #5279

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

Merged
merged 7 commits into from
Aug 13, 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
60 changes: 55 additions & 5 deletions packages-exp/auth-exp/src/core/auth/initialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,7 +128,8 @@ describe('core/auth/initialize', () => {
}
}

const fakePopupRedirectResolver: PopupRedirectResolver = FakePopupRedirectResolver;
const fakePopupRedirectResolver: PopupRedirectResolver =
FakePopupRedirectResolver;

before(() => {
registerAuth(ClientPlatform.BROWSER);
Expand Down Expand Up @@ -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();
});
});
});
13 changes: 9 additions & 4 deletions packages-exp/auth-exp/src/core/auth/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 ?? {})) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we are going to use reference equality for comparing builtin objects like popupRedirectResolvers and persistences. Using deepEqual feels unnecessary.

Copy link
Contributor Author

@hsubox76 hsubox76 Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mainly using deepEqual to avoid extra lines of code, but also, while 2 of the 3 options (popupRedirectResolver and errorMap) can be compared using a simple ===, persistence can be an array, and on top of that, an array where order matters.

return auth;
} else {
_fail(auth, AuthErrorCode.ALREADY_INITIALIZED);
}
}

const auth = provider.initialize({ options: deps }) as AuthImpl;
Expand All @@ -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<PersistenceInternal>(_getInstance);
if (deps?.errorMap) {
auth._updateErrorMap(deps.errorMap);
Expand Down
5 changes: 4 additions & 1 deletion packages-exp/auth-exp/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ function _debugErrorMap(): ErrorMap<AuthErrorCode> {
[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.'
};
}

Expand Down