Skip to content

Commit ba9b8e7

Browse files
committed
First pass at polyfill
1 parent 78df2a6 commit ba9b8e7

File tree

108 files changed

+1037
-458
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1037
-458
lines changed

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

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google LLC
3+
* Copyright 2020 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -15,6 +15,66 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { testFxn } from './src';
18+
import firebase from '@firebase/app';
19+
import { _FirebaseNamespace } from '@firebase/app-types/private';
20+
import * as externs from '@firebase/auth-types-exp';
21+
import {
22+
Component,
23+
ComponentType,
24+
InstantiationMode
25+
} from '@firebase/component';
26+
import '@firebase/installations';
27+
import { name, version } from './package.json';
28+
import { Auth } from './src/auth';
1929

20-
testFxn();
30+
const AUTH_TYPE = 'auth';
31+
32+
// Create auth components to register with firebase.
33+
// Provides Auth public APIs.
34+
function registerAuth(instance: _FirebaseNamespace): void {
35+
instance.INTERNAL.registerComponent(
36+
new Component(
37+
AUTH_TYPE,
38+
container => {
39+
// getImmediate for FirebaseApp will always succeed
40+
const app = container.getProvider('app').getImmediate();
41+
return new Auth(app);
42+
},
43+
ComponentType.PUBLIC
44+
)
45+
.setServiceProps({
46+
ActionCodeInfo: {
47+
Operation: {
48+
EMAIL_SIGNIN: externs.Operation.EMAIL_SIGNIN,
49+
PASSWORD_RESET: externs.Operation.PASSWORD_RESET,
50+
RECOVER_EMAIL: externs.Operation.RECOVER_EMAIL,
51+
REVERT_SECOND_FACTOR_ADDITION:
52+
externs.Operation.REVERT_SECOND_FACTOR_ADDITION,
53+
VERIFY_AND_CHANGE_EMAIL: externs.Operation.VERIFY_AND_CHANGE_EMAIL,
54+
VERIFY_EMAIL: externs.Operation.VERIFY_EMAIL
55+
}
56+
}
57+
// TODO(avolkovi): Expose the other top level properties
58+
// EmailAuthProvider,
59+
// FacebookAuthProvider,
60+
// GithubAuthProvider,
61+
// GoogleAuthProvider,
62+
// OAuthProvider,
63+
// SAMLAuthProvider,
64+
// PhoneAuthProvider,
65+
// RecaptchaVerifier,
66+
// TwitterAuthProvider,
67+
// Auth: {
68+
// Persistence
69+
// }
70+
// 'AuthCredential': fireauth.AuthCredential,
71+
// 'Error': fireauth.AuthError
72+
})
73+
.setInstantiationMode(InstantiationMode.LAZY)
74+
.setMultipleInstances(false)
75+
);
76+
77+
instance.registerVersion(name, version);
78+
}
79+
80+
registerAuth(firebase as _FirebaseNamespace);

packages-exp/auth-compat-exp/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
77
"main": "dist/index.node.cjs.js",
88
"browser": "dist/index.cjs.js",
9-
"module": "dist/index.esm.js",
9+
"module": "dist/index.esm5.js",
1010
"esm2017": "dist/index.esm2017.js",
1111
"react-native": "dist/index.rn.cjs.js",
1212
"files": [
@@ -25,10 +25,12 @@
2525
"prepare": "yarn build"
2626
},
2727
"peerDependencies": {
28-
"@firebase/app-exp": "0.x",
29-
"@firebase/app-types-exp": "0.x",
28+
"@firebase/app": "0.x",
3029
"@firebase/auth-exp": "0.x",
31-
"@firebase/auth-types-exp": "0.x"
30+
"@firebase/auth-types-exp": "0.x",
31+
"@firebase/component": "0.x",
32+
"@firebase/installations": "0.x",
33+
"@firebase/util": "0.x"
3234
},
3335
"dependencies": {
3436
"tslib": "1.11.1"

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import json from 'rollup-plugin-json';
1819
import typescriptPlugin from 'rollup-plugin-typescript2';
1920
import typescript from 'typescript';
2021
import pkg from './package.json';
@@ -23,10 +24,16 @@ const deps = Object.keys(
2324
Object.assign({}, pkg.peerDependencies, pkg.dependencies)
2425
);
2526

27+
/**
28+
* Common plugins for all builds
29+
*/
30+
const commonPlugins = [json()];
31+
2632
/**
2733
* ES5 Builds
2834
*/
2935
const es5BuildPlugins = [
36+
...commonPlugins,
3037
typescriptPlugin({
3138
typescript
3239
})
@@ -40,7 +47,7 @@ const es5Builds = [
4047
input: 'index.ts',
4148
output: [
4249
{ file: pkg.browser, format: 'cjs', sourcemap: true },
43-
{ file: pkg.module, format: 'es', sourcemap: true }
50+
{ file: pkg.module, format: 'esm', sourcemap: true }
4451
],
4552
plugins: es5BuildPlugins,
4653
external: id => deps.some(dep => id === dep || id.startsWith(`${dep}/`))
@@ -72,6 +79,7 @@ const es5Builds = [
7279
* ES2017 Builds
7380
*/
7481
const es2017BuildPlugins = [
82+
...commonPlugins,
7583
typescriptPlugin({
7684
typescript,
7785
tsconfigOverride: {
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { FirebaseApp } from '@firebase/app-types';
19+
import * as impl from '@firebase/auth-exp';
20+
import {
21+
AuthImplCompat,
22+
DEFAULT_API_HOST,
23+
DEFAULT_API_SCHEME,
24+
DEFAULT_TOKEN_API_HOST
25+
} from '@firebase/auth-exp/src/core/auth/auth_impl';
26+
import { AuthErrorCode } from '@firebase/auth-exp/src/core/errors';
27+
import { Persistence as persistenceImpl } from '@firebase/auth-exp/src/core/persistence';
28+
import { assert, fail } from '@firebase/auth-exp/src/core/util/assert';
29+
import { _getInstance } from '@firebase/auth-exp/src/core/util/instantiator';
30+
import {
31+
ClientPlatform,
32+
_getClientVersion
33+
} from '@firebase/auth-exp/src/core/util/version';
34+
import * as compat from '@firebase/auth-types';
35+
import * as externs from '@firebase/auth-types-exp';
36+
import '@firebase/installations';
37+
import { Observer, Unsubscribe, ErrorFn } from '@firebase/util';
38+
import { User } from './user';
39+
import {
40+
convertComfirmationResult,
41+
convertCredential
42+
} from './user_credential';
43+
44+
export class Auth extends AuthImplCompat implements compat.FirebaseAuth {
45+
readonly app: FirebaseApp;
46+
currentUser: User | null;
47+
48+
constructor(app: FirebaseApp) {
49+
const { apiKey, authDomain } = app.options;
50+
51+
const persistence = [impl.indexedDBLocalPersistence];
52+
const hierarchy = (Array.isArray(persistence)
53+
? persistence
54+
: [persistence]
55+
).map<persistenceImpl>(_getInstance);
56+
57+
// TODO: platform needs to be determined using heuristics
58+
assert(apiKey, app.name, AuthErrorCode.INVALID_API_KEY);
59+
const config: externs.Config = {
60+
apiKey,
61+
authDomain,
62+
apiHost: DEFAULT_API_HOST,
63+
tokenApiHost: DEFAULT_TOKEN_API_HOST,
64+
apiScheme: DEFAULT_API_SCHEME,
65+
sdkClientVersion: _getClientVersion(ClientPlatform.BROWSER)
66+
};
67+
68+
super(app.name, config);
69+
this.app = app;
70+
this.currentUser = null;
71+
72+
// This promise is intended to float; auth initialization happens in the
73+
// background, meanwhile the auth object may be used by the app.
74+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
75+
this._initializeWithPersistence(
76+
hierarchy,
77+
impl.browserPopupRedirectResolver
78+
);
79+
}
80+
81+
applyActionCode(code: string): Promise<void> {
82+
return impl.applyActionCode(this, code);
83+
}
84+
85+
checkActionCode(code: string): Promise<compat.ActionCodeInfo> {
86+
return impl.checkActionCode(this, code);
87+
}
88+
89+
confirmPasswordReset(code: string, newPassword: string): Promise<void> {
90+
return impl.confirmPasswordReset(this, code, newPassword);
91+
}
92+
93+
async createUserWithEmailAndPassword(
94+
email: string,
95+
password: string
96+
): Promise<compat.UserCredential> {
97+
return convertCredential(
98+
impl.createUserWithEmailAndPassword(this, email, password)
99+
);
100+
}
101+
// currentUser: User | null;
102+
fetchSignInMethodsForEmail(email: string): Promise<string[]> {
103+
return impl.fetchSignInMethodsForEmail(this, email);
104+
}
105+
isSignInWithEmailLink(emailLink: string): boolean {
106+
return impl.isSignInWithEmailLink(this, emailLink);
107+
}
108+
async getRedirectResult(): Promise<compat.UserCredential> {
109+
const credential = await impl.getRedirectResult(
110+
this,
111+
impl.browserPopupRedirectResolver
112+
);
113+
if (!credential) {
114+
return {
115+
credential: null,
116+
user: null
117+
};
118+
}
119+
return convertCredential(Promise.resolve(credential!));
120+
}
121+
// languageCode: string | null;
122+
// settings: AuthSettings;
123+
onAuthStateChanged(
124+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
125+
nextOrObserver: Observer<any> | ((a: compat.User | null) => any),
126+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
127+
error?: (error: compat.Error) => any,
128+
completed?: Unsubscribe
129+
): Unsubscribe {
130+
return super._onAuthStateChanged(
131+
nextOrObserver as externs.NextOrObserver<externs.User | null>,
132+
error as ErrorFn,
133+
completed
134+
);
135+
}
136+
onIdTokenChanged(
137+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
138+
nextOrObserver: Observer<any> | ((a: compat.User | null) => any),
139+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
140+
error?: (error: compat.Error) => any,
141+
completed?: Unsubscribe
142+
): Unsubscribe {
143+
return super._onIdTokenChanged(
144+
nextOrObserver as externs.NextOrObserver<externs.User | null>,
145+
error as ErrorFn,
146+
completed
147+
);
148+
}
149+
sendSignInLinkToEmail(
150+
email: string,
151+
actionCodeSettings: compat.ActionCodeSettings
152+
): Promise<void> {
153+
return impl.sendSignInLinkToEmail(this, email, actionCodeSettings);
154+
}
155+
sendPasswordResetEmail(
156+
email: string,
157+
actionCodeSettings?: compat.ActionCodeSettings | null
158+
): Promise<void> {
159+
return impl.sendPasswordResetEmail(
160+
this,
161+
email,
162+
actionCodeSettings || undefined
163+
);
164+
}
165+
async setPersistence(persistence: string): Promise<void> {
166+
function convertPersistence(
167+
auth: externs.Auth,
168+
persistenceCompat: string
169+
): externs.Persistence {
170+
switch (persistenceCompat) {
171+
case compat.FirebaseAuth.Persistence.LOCAL:
172+
return impl.browserLocalPersistence;
173+
case compat.FirebaseAuth.Persistence.SESSION:
174+
return impl.browserSessionPersistence;
175+
case compat.FirebaseAuth.Persistence.NONE:
176+
return impl.inMemoryPersistence;
177+
default:
178+
fail(auth.name, AuthErrorCode.ARGUMENT_ERROR);
179+
}
180+
}
181+
182+
return super._setPersistence(
183+
_getInstance(convertPersistence(this, persistence))
184+
);
185+
}
186+
187+
signInAndRetrieveDataWithCredential(
188+
credential: compat.AuthCredential
189+
): Promise<compat.UserCredential> {
190+
return this.signInWithCredential(credential);
191+
}
192+
signInAnonymously(): Promise<compat.UserCredential> {
193+
return convertCredential(impl.signInAnonymously(this));
194+
}
195+
signInWithCredential(
196+
credential: compat.AuthCredential
197+
): Promise<compat.UserCredential> {
198+
return convertCredential(
199+
impl.signInWithCredential(this, credential as externs.AuthCredential)
200+
);
201+
}
202+
signInWithCustomToken(token: string): Promise<compat.UserCredential> {
203+
return convertCredential(impl.signInWithCustomToken(this, token));
204+
}
205+
signInWithEmailAndPassword(
206+
email: string,
207+
password: string
208+
): Promise<compat.UserCredential> {
209+
return convertCredential(
210+
impl.signInWithEmailAndPassword(this, email, password)
211+
);
212+
}
213+
signInWithEmailLink(
214+
email: string,
215+
emailLink?: string
216+
): Promise<compat.UserCredential> {
217+
return convertCredential(impl.signInWithEmailLink(this, email, emailLink));
218+
}
219+
signInWithPhoneNumber(
220+
phoneNumber: string,
221+
applicationVerifier: compat.ApplicationVerifier
222+
): Promise<compat.ConfirmationResult> {
223+
return convertComfirmationResult(
224+
impl.signInWithPhoneNumber(this, phoneNumber, applicationVerifier)
225+
);
226+
}
227+
signInWithPopup(
228+
provider: compat.AuthProvider
229+
): Promise<compat.UserCredential> {
230+
return convertCredential(
231+
impl.signInWithPopup(
232+
this,
233+
provider as externs.AuthProvider,
234+
impl.browserLocalPersistence
235+
)
236+
);
237+
}
238+
signInWithRedirect(provider: compat.AuthProvider): Promise<void> {
239+
return impl.signInWithRedirect(
240+
this,
241+
provider as externs.AuthProvider,
242+
impl.browserPopupRedirectResolver
243+
);
244+
}
245+
updateCurrentUser(user: User | null): Promise<void> {
246+
return super.updateCurrentUser(user);
247+
}
248+
verifyPasswordResetCode(code: string): Promise<string> {
249+
return impl.verifyPasswordResetCode(this, code);
250+
}
251+
}

0 commit comments

Comments
 (0)