Skip to content

Commit 7da9610

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

File tree

16 files changed

+406
-86
lines changed

16 files changed

+406
-86
lines changed

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

Lines changed: 57 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,60 @@
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 './scr/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(AUTH_TYPE, container => {
37+
// getImmediate for FirebaseApp will always succeed
38+
const app = container.getProvider('app').getImmediate();
39+
return new Auth(app);
40+
},
41+
ComponentType.PUBLIC
42+
).setServiceProps({
43+
ActionCodeInfo: {
44+
Operation: {
45+
EMAIL_SIGNIN: externs.Operation.EMAIL_SIGNIN,
46+
PASSWORD_RESET: externs.Operation.PASSWORD_RESET,
47+
RECOVER_EMAIL: externs.Operation.RECOVER_EMAIL,
48+
REVERT_SECOND_FACTOR_ADDITION: externs.Operation.REVERT_SECOND_FACTOR_ADDITION,
49+
VERIFY_AND_CHANGE_EMAIL: externs.Operation.VERIFY_AND_CHANGE_EMAIL,
50+
VERIFY_EMAIL: externs.Operation.VERIFY_EMAIL
51+
}
52+
}
53+
// TODO(avolkovi): Expose the other top level properties
54+
// EmailAuthProvider,
55+
// FacebookAuthProvider,
56+
// GithubAuthProvider,
57+
// GoogleAuthProvider,
58+
// OAuthProvider,
59+
// SAMLAuthProvider,
60+
// PhoneAuthProvider,
61+
// RecaptchaVerifier,
62+
// TwitterAuthProvider,
63+
// Auth: {
64+
// Persistence
65+
// }
66+
// 'AuthCredential': fireauth.AuthCredential,
67+
// 'Error': fireauth.AuthError
68+
}).setInstantiationMode(InstantiationMode.LAZY).setMultipleInstances(false)
69+
);
70+
71+
instance.registerVersion(name, version);
72+
}
73+
74+
registerAuth(firebase as _FirebaseNamespace);

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

Lines changed: 5 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,11 @@
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"
3233
},
3334
"dependencies": {
3435
"tslib": "1.11.1"
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 * as compat from '@firebase/auth-types';
21+
import * as externs from '@firebase/auth-types-exp';
22+
import '@firebase/installations';
23+
import { User } from './src/user';
24+
25+
export class Auth implements compat.FirebaseAuth {
26+
readonly auth: externs.Auth;
27+
constructor(readonly app: FirebaseApp) {
28+
this.auth = impl.initializeAuth(app);
29+
}
30+
31+
applyActionCode(code: string): Promise<void> {
32+
return impl.applyActionCode(this.auth, code);
33+
}
34+
35+
checkActionCode(code: string): Promise<compat.ActionCodeInfo> {
36+
return impl.checkActionCode(this.auth, code);
37+
}
38+
39+
confirmPasswordReset(code: string, newPassword: string): Promise<void> {
40+
return impl.confirmPasswordReset(this.auth, code, newPassword);
41+
}
42+
43+
async createUserWithEmailAndPassword(email: string, password: string): Promise<compat.UserCredential> {
44+
const userCred = await impl.createUserWithEmailAndPassword(this.auth, email, password);
45+
const credential = impl.EmailAuthProvider.credential(email, password);
46+
return {
47+
credential,
48+
operationType: userCred.operationType,
49+
user: new User(userCred.user)
50+
};
51+
}
52+
// currentUser: User | null;
53+
// fetchSignInMethodsForEmail(email: string): Promise<Array<string>>;
54+
// isSignInWithEmailLink(emailLink: string): boolean;
55+
// getRedirectResult(): Promise<UserCredential>;
56+
// languageCode: string | null;
57+
// settings: AuthSettings;
58+
// onAuthStateChanged(
59+
// nextOrObserver: Observer<any> | ((a: User | null) => any),
60+
// error?: (a: Error) => any,
61+
// completed?: Unsubscribe
62+
// ): Unsubscribe;
63+
// onIdTokenChanged(
64+
// nextOrObserver: Observer<any> | ((a: User | null) => any),
65+
// error?: (a: Error) => any,
66+
// completed?: Unsubscribe
67+
// ): Unsubscribe;
68+
// sendSignInLinkToEmail(
69+
// email: string,
70+
// actionCodeSettings: ActionCodeSettings
71+
// ): Promise<void>;
72+
// sendPasswordResetEmail(
73+
// email: string,
74+
// actionCodeSettings?: ActionCodeSettings | null
75+
// ): Promise<void>;
76+
// setPersistence(persistence: Persistence): Promise<void>;
77+
// signInAndRetrieveDataWithCredential(
78+
// credential: AuthCredential
79+
// ): Promise<UserCredential>;
80+
// signInAnonymously(): Promise<UserCredential>;
81+
// signInWithCredential(credential: AuthCredential): Promise<UserCredential>;
82+
// signInWithCustomToken(token: string): Promise<UserCredential>;
83+
// signInWithEmailAndPassword(
84+
// email: string,
85+
// password: string
86+
// ): Promise<UserCredential>;
87+
// signInWithEmailLink(
88+
// email: string,
89+
// emailLink?: string
90+
// ): Promise<UserCredential>;
91+
// signInWithPhoneNumber(
92+
// phoneNumber: string,
93+
// applicationVerifier: ApplicationVerifier
94+
// ): Promise<ConfirmationResult>;
95+
// signInWithPopup(provider: AuthProvider): Promise<UserCredential>;
96+
// signInWithRedirect(provider: AuthProvider): Promise<void>;
97+
// signOut(): Promise<void>;
98+
// tenantId: string | null;
99+
// updateCurrentUser(user: User | null): Promise<void>;
100+
// useDeviceLanguage(): void;
101+
// verifyPasswordResetCode(code: string): Promise<string>;
102+
}

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

Lines changed: 0 additions & 31 deletions
This file was deleted.

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 * as impl from '@firebase/auth-exp';
19+
import { UserImpl } from '@firebase/auth-exp/src/core/user/user_impl';
20+
import { UserParameters } from '@firebase/auth-exp/src/model/user';
21+
import * as compat from '@firebase/auth-types';
22+
import * as externs from '@firebase/auth-types-exp';
23+
import '@firebase/installations';
24+
import { convertComfirmationResult, convertCredential } from './user_credential';
25+
26+
export class User extends UserImpl implements compat.User {
27+
readonly multiFactor: compat.MultiFactorUser;
28+
29+
constructor(params: UserParameters) {
30+
super(params);
31+
this.multiFactor = impl.multiFactor(this);
32+
}
33+
34+
getIdTokenResult(forceRefresh?: boolean): Promise<compat.IdTokenResult> {
35+
return impl.getIdTokenResult(this, forceRefresh);
36+
}
37+
getIdToken(forceRefresh?: boolean): Promise<string> {
38+
return impl.getIdToken(this, forceRefresh);
39+
}
40+
linkAndRetrieveDataWithCredential(
41+
credential: compat.AuthCredential
42+
): Promise<compat.UserCredential> {
43+
return this.linkWithCredential(credential);
44+
}
45+
async linkWithCredential(credential: compat.AuthCredential): Promise<compat.UserCredential> {
46+
return convertCredential(impl.linkWithCredential(this, credential as externs.AuthCredential));
47+
}
48+
async linkWithPhoneNumber(
49+
phoneNumber: string,
50+
applicationVerifier: compat.ApplicationVerifier
51+
): Promise<compat.ConfirmationResult> {
52+
return convertComfirmationResult(impl.linkWithPhoneNumber(this, phoneNumber, applicationVerifier));
53+
}
54+
async linkWithPopup(provider: compat.AuthProvider): Promise<compat.UserCredential> {
55+
return convertCredential(impl.linkWithPopup(this, provider as externs.AuthProvider, impl.browserPopupRedirectResolver));
56+
}
57+
linkWithRedirect(provider: compat.AuthProvider): Promise<void> {
58+
return impl.linkWithRedirect(this, provider as externs.AuthProvider, impl.browserPopupRedirectResolver);
59+
}
60+
reauthenticateAndRetrieveDataWithCredential(
61+
credential: compat.AuthCredential
62+
): Promise<compat.UserCredential> {
63+
return this.reauthenticateWithCredential(credential);
64+
}
65+
async reauthenticateWithCredential(
66+
credential: compat.AuthCredential
67+
): Promise<compat.UserCredential> {
68+
return convertCredential(impl.reauthenticateWithCredential(this, credential as externs.AuthCredential));
69+
}
70+
reauthenticateWithPhoneNumber(
71+
phoneNumber: string,
72+
applicationVerifier: compat.ApplicationVerifier
73+
): Promise<compat.ConfirmationResult> {
74+
return convertComfirmationResult(impl.reauthenticateWithPhoneNumber(this, phoneNumber, applicationVerifier));
75+
}
76+
reauthenticateWithPopup(provider: compat.AuthProvider): Promise<compat.UserCredential> {
77+
return convertCredential(impl.reauthenticateWithPopup(this, provider as externs.AuthProvider, impl.browserPopupRedirectResolver));
78+
}
79+
reauthenticateWithRedirect(provider: compat.AuthProvider): Promise<void> {
80+
return impl.reauthenticateWithRedirect(this, provider as externs.AuthProvider, impl.browserPopupRedirectResolver);
81+
}
82+
sendEmailVerification(
83+
actionCodeSettings?: compat.ActionCodeSettings | null
84+
): Promise<void> {
85+
return impl.sendEmailVerification(this, actionCodeSettings);
86+
}
87+
async unlink(providerId: string): Promise<compat.User> {
88+
await impl.unlink(this, providerId as externs.ProviderId);
89+
return this;
90+
}
91+
updateEmail(newEmail: string): Promise<void> {
92+
return impl.updateEmail(this, newEmail);
93+
}
94+
updatePassword(newPassword: string): Promise<void> {
95+
return impl.updatePassword(this, newPassword);
96+
}
97+
updatePhoneNumber(phoneCredential: compat.AuthCredential): Promise<void> {
98+
return impl.updatePhoneNumber(this, phoneCredential as externs.AuthCredential);
99+
}
100+
updateProfile(profile: {
101+
displayName?: string | null;
102+
photoURL?: string | null;
103+
}): Promise<void> {
104+
return impl.updateProfile(this, profile);
105+
}
106+
verifyBeforeUpdateEmail(
107+
newEmail: string,
108+
actionCodeSettings?: compat.ActionCodeSettings | null
109+
): Promise<void> {
110+
return impl.verifyBeforeUpdateEmail(this, newEmail, actionCodeSettings);
111+
}
112+
}

0 commit comments

Comments
 (0)