Skip to content

Commit fdadf71

Browse files
authored
[Auth compat] Fix the way we initialize compat auth. Also fix some broken integration tests (#4653)
* Fix auth compat initialization * Fix compat lint * Fix broken node tests * Formatting * Remove dead code
1 parent 8c56c25 commit fdadf71

File tree

8 files changed

+65
-49
lines changed

8 files changed

+65
-49
lines changed

packages-exp/auth-compat-exp/index.node.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
export * from './index';
2525
import { FetchProvider } from '@firebase/auth-exp/internal';
2626
import * as fetchImpl from 'node-fetch';
27+
import './index';
2728

2829
FetchProvider.initialize(
2930
(fetchImpl.default as unknown) as typeof fetch,

packages-exp/auth-compat-exp/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ function registerAuthCompat(instance: _FirebaseNamespace): void {
9898
container => {
9999
// getImmediate for FirebaseApp will always succeed
100100
const app = container.getProvider('app-compat').getImmediate();
101-
const auth = container.getProvider('auth-exp').getImmediate();
102-
return new Auth(app, auth as impl.AuthImpl);
101+
const authProvider = container.getProvider('auth-exp');
102+
return new Auth(app, authProvider);
103103
},
104104
ComponentType.PUBLIC
105105
)
@@ -136,7 +136,7 @@ function registerAuthCompat(instance: _FirebaseNamespace): void {
136136
.setMultipleInstances(false)
137137
);
138138

139-
instance.registerVersion('auth', version);
139+
instance.registerVersion('auth-compat', version);
140140
}
141141

142142
registerAuthCompat(firebase as _FirebaseNamespace);

packages-exp/auth-compat-exp/rollup.config.shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function getEs5Builds(additionalTypescriptPlugins = {}) {
6565
plugins: es5BuildPlugins,
6666
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`)),
6767
treeshake: {
68-
moduleSideEffects: false
68+
moduleSideEffects: true
6969
}
7070
},
7171
/**

packages-exp/auth-compat-exp/src/auth.test.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { FirebaseApp } from '@firebase/app-compat';
1919
import * as exp from '@firebase/auth-exp/internal';
20+
import { Provider } from '@firebase/component';
2021
import { expect, use } from 'chai';
2122
import * as sinon from 'sinon';
2223
import * as sinonChai from 'sinon-chai';
@@ -31,12 +32,16 @@ describe('auth compat', () => {
3132
context('redirect persistence key storage', () => {
3233
let underlyingAuth: exp.AuthImpl;
3334
let app: FirebaseApp;
35+
let providerStub: sinon.SinonStubbedInstance<Provider<'auth-exp'>>;
36+
3437
beforeEach(() => {
3538
app = { options: { apiKey: 'api-key' } } as FirebaseApp;
3639
underlyingAuth = new exp.AuthImpl(app, {
3740
apiKey: 'api-key'
3841
} as exp.Config);
3942
sinon.stub(underlyingAuth, '_initializeWithPersistence');
43+
44+
providerStub = sinon.createStubInstance(Provider);
4045
});
4146

4247
afterEach(() => {
@@ -56,7 +61,12 @@ describe('auth compat', () => {
5661
),
5762
'_openRedirect'
5863
);
59-
const authCompat = new Auth(app, underlyingAuth);
64+
providerStub.isInitialized.returns(true);
65+
providerStub.getImmediate.returns(underlyingAuth);
66+
const authCompat = new Auth(
67+
app,
68+
(providerStub as unknown) as Provider<'auth-exp'>
69+
);
6070
// eslint-disable-next-line @typescript-eslint/no-floating-promises
6171
await authCompat.signInWithRedirect(new exp.GoogleAuthProvider());
6272
expect(
@@ -71,18 +81,20 @@ describe('auth compat', () => {
7181
'firebase:persistence:api-key:undefined',
7282
'none'
7383
);
74-
new Auth(app, underlyingAuth);
84+
providerStub.isInitialized.returns(false);
85+
providerStub.initialize.returns(underlyingAuth);
86+
new Auth(app, (providerStub as unknown) as Provider<'auth-exp'>);
7587
// eslint-disable-next-line @typescript-eslint/no-floating-promises
76-
expect(
77-
underlyingAuth._initializeWithPersistence
78-
).to.have.been.calledWith(
79-
[
80-
exp._getInstance(exp.inMemoryPersistence),
81-
exp._getInstance(exp.indexedDBLocalPersistence),
82-
exp._getInstance(exp.browserLocalPersistence)
83-
],
84-
CompatPopupRedirectResolver
85-
);
88+
expect(providerStub.initialize).to.have.been.calledWith({
89+
options: {
90+
popupRedirectResolver: CompatPopupRedirectResolver,
91+
persistence: [
92+
exp.inMemoryPersistence,
93+
exp.indexedDBLocalPersistence,
94+
exp.browserLocalPersistence
95+
]
96+
}
97+
});
8698
}
8799
});
88100
});

packages-exp/auth-compat-exp/src/auth.ts

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { FirebaseApp, _FirebaseService } from '@firebase/app-compat';
1919
import * as exp from '@firebase/auth-exp/internal';
2020
import * as compat from '@firebase/auth-types';
21+
import { Provider } from '@firebase/component';
2122
import { ErrorFn, Observer, Unsubscribe } from '@firebase/util';
2223

2324
import {
@@ -39,47 +40,55 @@ const _assert: typeof exp._assert = exp._assert;
3940

4041
export class Auth
4142
implements compat.FirebaseAuth, Wrapper<exp.Auth>, _FirebaseService {
42-
// private readonly auth: impl.AuthImpl;
43+
private readonly auth: exp.AuthImpl;
4344

44-
constructor(readonly app: FirebaseApp, private readonly auth: exp.AuthImpl) {
45-
const { apiKey } = app.options;
46-
if (this.auth._deleted) {
45+
constructor(readonly app: FirebaseApp, provider: Provider<'auth-exp'>) {
46+
if (provider.isInitialized()) {
47+
this.auth = provider.getImmediate() as exp.AuthImpl;
4748
return;
4849
}
4950

50-
// Note this is slightly different behavior: in this case, the stored
51-
// persistence is checked *first* rather than last. This is because we want
52-
// to prefer stored persistence type in the hierarchy.
53-
const persistences = _getPersistencesFromRedirect(this.auth);
51+
const { apiKey } = app.options;
52+
// TODO: platform needs to be determined using heuristics
53+
_assert(apiKey, exp.AuthErrorCode.INVALID_API_KEY, {
54+
appName: app.name
55+
});
56+
57+
let persistences: exp.Persistence[] = [exp.inMemoryPersistence];
5458

55-
for (const persistence of [
56-
exp.indexedDBLocalPersistence,
57-
exp.browserLocalPersistence
58-
]) {
59-
if (!persistences.includes(persistence)) {
60-
persistences.push(persistence);
59+
// Only deal with persistences in web environments
60+
if (typeof window !== 'undefined') {
61+
// Note this is slightly different behavior: in this case, the stored
62+
// persistence is checked *first* rather than last. This is because we want
63+
// to prefer stored persistence type in the hierarchy.
64+
persistences = _getPersistencesFromRedirect(apiKey, app.name);
65+
66+
for (const persistence of [
67+
exp.indexedDBLocalPersistence,
68+
exp.browserLocalPersistence
69+
]) {
70+
if (!persistences.includes(persistence)) {
71+
persistences.push(persistence);
72+
}
6173
}
6274
}
6375

64-
const hierarchy = persistences.map<exp.PersistenceInternal>(
65-
exp._getInstance
66-
);
67-
6876
// TODO: platform needs to be determined using heuristics
6977
_assert(apiKey, exp.AuthErrorCode.INVALID_API_KEY, {
7078
appName: app.name
7179
});
7280

73-
this.auth._updateErrorMap(exp.debugErrorMap);
74-
7581
// Only use a popup/redirect resolver in browser environments
7682
const resolver =
7783
typeof window !== 'undefined' ? CompatPopupRedirectResolver : undefined;
84+
this.auth = provider.initialize({
85+
options: {
86+
persistence: persistences,
87+
popupRedirectResolver: resolver
88+
}
89+
}) as exp.AuthImpl;
7890

79-
// This promise is intended to float; auth initialization happens in the
80-
// background, meanwhile the auth object may be used by the app.
81-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
82-
this.auth._initializeWithPersistence(hierarchy, resolver);
91+
this.auth._updateErrorMap(exp.debugErrorMap);
8392
}
8493

8594
get emulatorConfig(): compat.EmulatorConfig | null {

packages-exp/auth-compat-exp/src/persistence.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,15 @@ export async function _savePersistenceForRedirect(
9797
}
9898

9999
export function _getPersistencesFromRedirect(
100-
auth: exp.AuthInternal
100+
apiKey: string,
101+
appName: string
101102
): exp.Persistence[] {
102103
const win = getSelfWindow();
103104
if (!win?.sessionStorage) {
104105
return [];
105106
}
106107

107-
const key = exp._persistenceKeyName(
108-
PERSISTENCE_KEY,
109-
auth.config.apiKey,
110-
auth.name
111-
);
108+
const key = exp._persistenceKeyName(PERSISTENCE_KEY, apiKey, appName);
112109
const persistence = win.sessionStorage.getItem(key);
113110

114111
switch (persistence) {

packages-exp/auth-compat-exp/test/integration/flows/anonymous.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import { expect, use } from 'chai';
1919
import * as chaiAsPromised from 'chai-as-promised';
2020

2121
import firebase from '@firebase/app-compat';
22-
// eslint-disable-next-line import/no-extraneous-dependencies
23-
import '@firebase/auth-compat';
2422
import { FirebaseError } from '@firebase/util';
2523
import {
2624
cleanUpTestInstance,

packages-exp/auth-compat-exp/test/integration/flows/custom.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { FirebaseError } from '@firebase/util';
1919
import { expect, use } from 'chai';
2020
import firebase from '@firebase/app-compat';
21-
import '@firebase/auth-compat';
2221

2322
import * as chaiAsPromised from 'chai-as-promised';
2423
import {

0 commit comments

Comments
 (0)