Skip to content

Commit d9eb81e

Browse files
committed
refactor(core): Align current API with new spec
1 parent e4525a8 commit d9eb81e

File tree

10 files changed

+49
-115
lines changed

10 files changed

+49
-115
lines changed

packages/core/src/auth.ts

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
AuthProvider,
2626
ConfirmationResult,
2727
EmailAuthProvider,
28-
getAuth,
2928
linkWithCredential,
3029
PhoneAuthProvider,
3130
RecaptchaVerifier,
@@ -43,7 +42,7 @@ async function handlePendingCredential(ui: FirebaseUIConfiguration, user: UserCr
4342

4443
try {
4544
const pendingCred = JSON.parse(pendingCredString);
46-
ui.setState("linking");
45+
ui.setState("pending");
4746
const result = await linkWithCredential(user.user, pendingCred);
4847
ui.setState("idle");
4948
window.sessionStorage.removeItem("pendingCred");
@@ -60,7 +59,6 @@ export async function signInWithEmailAndPassword(
6059
password: string
6160
): Promise<UserCredential> {
6261
try {
63-
const auth = getAuth(ui.app);
6462
const credential = EmailAuthProvider.credential(email, password);
6563

6664
if (hasBehavior(ui, "autoUpgradeAnonymousCredential")) {
@@ -71,8 +69,8 @@ export async function signInWithEmailAndPassword(
7169
}
7270
}
7371

74-
ui.setState("signing-in");
75-
const result = await signInWithCredential(auth, credential);
72+
ui.setState("pending");
73+
const result = await signInWithCredential(ui.auth, credential);
7674
return handlePendingCredential(ui, result);
7775
} catch (error) {
7876
handleFirebaseError(ui, error);
@@ -87,7 +85,6 @@ export async function createUserWithEmailAndPassword(
8785
password: string
8886
): Promise<UserCredential> {
8987
try {
90-
const auth = getAuth(ui.app);
9188
const credential = EmailAuthProvider.credential(email, password);
9289

9390
if (hasBehavior(ui, "autoUpgradeAnonymousCredential")) {
@@ -98,8 +95,8 @@ export async function createUserWithEmailAndPassword(
9895
}
9996
}
10097

101-
ui.setState("creating-user");
102-
const result = await _createUserWithEmailAndPassword(auth, email, password);
98+
ui.setState("pending");
99+
const result = await _createUserWithEmailAndPassword(ui.auth, email, password);
103100
return handlePendingCredential(ui, result);
104101
} catch (error) {
105102
handleFirebaseError(ui, error);
@@ -114,9 +111,8 @@ export async function signInWithPhoneNumber(
114111
recaptchaVerifier: RecaptchaVerifier
115112
): Promise<ConfirmationResult> {
116113
try {
117-
const auth = getAuth(ui.app);
118-
ui.setState("signing-in");
119-
return await _signInWithPhoneNumber(auth, phoneNumber, recaptchaVerifier);
114+
ui.setState("pending");
115+
return await _signInWithPhoneNumber(ui.auth, phoneNumber, recaptchaVerifier);
120116
} catch (error) {
121117
handleFirebaseError(ui, error);
122118
} finally {
@@ -130,8 +126,7 @@ export async function confirmPhoneNumber(
130126
verificationCode: string
131127
): Promise<UserCredential> {
132128
try {
133-
const auth = getAuth(ui.app);
134-
const currentUser = auth.currentUser;
129+
const currentUser = ui.auth.currentUser;
135130
const credential = PhoneAuthProvider.credential(confirmationResult.verificationId, verificationCode);
136131

137132
if (currentUser?.isAnonymous && hasBehavior(ui, "autoUpgradeAnonymousCredential")) {
@@ -142,8 +137,8 @@ export async function confirmPhoneNumber(
142137
}
143138
}
144139

145-
ui.setState("signing-in");
146-
const result = await signInWithCredential(auth, credential);
140+
ui.setState("pending");
141+
const result = await signInWithCredential(ui.auth, credential);
147142
return handlePendingCredential(ui, result);
148143
} catch (error) {
149144
handleFirebaseError(ui, error);
@@ -154,9 +149,8 @@ export async function confirmPhoneNumber(
154149

155150
export async function sendPasswordResetEmail(ui: FirebaseUIConfiguration, email: string): Promise<void> {
156151
try {
157-
const auth = getAuth(ui.app);
158-
ui.setState("sending-password-reset-email");
159-
await _sendPasswordResetEmail(auth, email);
152+
ui.setState("pending");
153+
await _sendPasswordResetEmail(ui.auth, email);
160154
} catch (error) {
161155
handleFirebaseError(ui, error);
162156
} finally {
@@ -166,16 +160,14 @@ export async function sendPasswordResetEmail(ui: FirebaseUIConfiguration, email:
166160

167161
export async function sendSignInLinkToEmail(ui: FirebaseUIConfiguration, email: string): Promise<void> {
168162
try {
169-
const auth = getAuth(ui.app);
170-
171163
const actionCodeSettings = {
172164
url: window.location.href,
173165
// TODO(ehesp): Check this...
174166
handleCodeInApp: true,
175167
} satisfies ActionCodeSettings;
176168

177-
ui.setState("sending-sign-in-link-to-email");
178-
await _sendSignInLinkToEmail(auth, email, actionCodeSettings);
169+
ui.setState("pending");
170+
await _sendSignInLinkToEmail(ui.auth, email, actionCodeSettings);
179171
window.localStorage.setItem("emailForSignIn", email);
180172
} catch (error) {
181173
handleFirebaseError(ui, error);
@@ -190,7 +182,6 @@ export async function signInWithEmailLink(
190182
link: string
191183
): Promise<UserCredential> {
192184
try {
193-
const auth = ui.getAuth();
194185
const credential = EmailAuthProvider.credentialWithLink(email, link);
195186

196187
if (hasBehavior(ui, "autoUpgradeAnonymousCredential")) {
@@ -200,8 +191,8 @@ export async function signInWithEmailLink(
200191
}
201192
}
202193

203-
ui.setState("signing-in");
204-
const result = await signInWithCredential(auth, credential);
194+
ui.setState("pending");
195+
const result = await signInWithCredential(ui.auth, credential);
205196
return handlePendingCredential(ui, result);
206197
} catch (error) {
207198
handleFirebaseError(ui, error);
@@ -212,9 +203,8 @@ export async function signInWithEmailLink(
212203

213204
export async function signInAnonymously(ui: FirebaseUIConfiguration): Promise<UserCredential> {
214205
try {
215-
const auth = getAuth(ui.app);
216-
ui.setState("signing-in");
217-
const result = await _signInAnonymously(auth);
206+
ui.setState("pending");
207+
const result = await _signInAnonymously(ui.auth);
218208
return handlePendingCredential(ui, result);
219209
} catch (error) {
220210
handleFirebaseError(ui, error);
@@ -225,16 +215,14 @@ export async function signInAnonymously(ui: FirebaseUIConfiguration): Promise<Us
225215

226216
export async function signInWithOAuth(ui: FirebaseUIConfiguration, provider: AuthProvider): Promise<void> {
227217
try {
228-
const auth = getAuth(ui.app);
229-
230218
if (hasBehavior(ui, "autoUpgradeAnonymousProvider")) {
231219
await getBehavior(ui, "autoUpgradeAnonymousProvider")(ui, provider);
232220
// If we get to here, the user is not anonymous, otherwise they
233221
// have been redirected to the provider's sign in page.
234222
}
235223

236-
ui.setState("signing-in");
237-
await signInWithRedirect(auth, provider);
224+
ui.setState("pending");
225+
await signInWithRedirect(ui.auth, provider);
238226
// We don't modify state here since the user is redirected.
239227
// If we support popups, we'd need to modify state here.
240228
} catch (error) {
@@ -249,15 +237,14 @@ export async function completeEmailLinkSignIn(
249237
currentUrl: string
250238
): Promise<UserCredential | null> {
251239
try {
252-
const auth = ui.getAuth();
253-
if (!_isSignInWithEmailLink(auth, currentUrl)) {
240+
if (!_isSignInWithEmailLink(ui.auth, currentUrl)) {
254241
return null;
255242
}
256243

257244
const email = window.localStorage.getItem("emailForSignIn");
258245
if (!email) return null;
259246

260-
ui.setState("signing-in");
247+
ui.setState("pending");
261248
const result = await signInWithEmailLink(ui, email, currentUrl);
262249
ui.setState("idle");
263250
return handlePendingCredential(ui, result);

packages/core/src/behaviors.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,17 @@ export function autoAnonymousLogin(): Behavior<"autoAnonymousLogin"> {
6666

6767
return {
6868
autoAnonymousLogin: async (ui) => {
69-
const auth = ui.getAuth();
70-
71-
const user = await new Promise<User>((resolve) => {
72-
const unsubscribe = onAuthStateChanged(auth, (user) => {
73-
ui.setState("signing-in");
74-
if (!user) {
75-
signInAnonymously(auth);
76-
return;
77-
}
78-
79-
unsubscribe();
80-
resolve(user);
81-
});
82-
});
69+
const auth = ui.auth;
70+
71+
await auth.authStateReady();
72+
73+
if (!auth.currentUser) {
74+
ui.setState("loading");
75+
await signInAnonymously(auth);
76+
}
77+
8378
ui.setState("idle");
84-
return user;
79+
return auth.currentUser!;
8580
},
8681
};
8782
}
@@ -91,28 +86,26 @@ export function autoUpgradeAnonymousUsers(): Behavior<
9186
> {
9287
return {
9388
autoUpgradeAnonymousCredential: async (ui, credential) => {
94-
const auth = ui.getAuth();
95-
const currentUser = auth.currentUser;
89+
const currentUser = ui.auth.currentUser;
9690

9791
// Check if the user is anonymous. If not, we can't upgrade them.
9892
if (!currentUser?.isAnonymous) {
9993
return;
10094
}
10195

102-
ui.setState("linking");
96+
ui.setState("pending");
10397
const result = await linkWithCredential(currentUser, credential);
10498
ui.setState("idle");
10599
return result;
106100
},
107101
autoUpgradeAnonymousProvider: async (ui, provider) => {
108-
const auth = ui.getAuth();
109-
const currentUser = auth.currentUser;
102+
const currentUser = ui.auth.currentUser;
110103

111104
if (!currentUser?.isAnonymous) {
112105
return;
113106
}
114107

115-
ui.setState("linking");
108+
ui.setState("pending");
116109
await linkWithRedirect(currentUser, provider);
117110
// We don't modify state here since the user is redirected.
118111
// If we support popups, we'd need to modify state here.

packages/core/src/config.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ import { FirebaseUIState } from "./state";
2323

2424
type FirebaseUIConfigurationOptions = {
2525
app: FirebaseApp;
26+
auth?: Auth;
2627
locale?: RegisteredLocale;
2728
behaviors?: Partial<Behavior<keyof BehaviorHandlers>>[];
28-
recaptchaMode?: "normal" | "invisible";
2929
};
3030

3131
export type FirebaseUIConfiguration = {
3232
app: FirebaseApp;
33-
getAuth: () => Auth;
33+
auth: Auth;
3434
setLocale: (locale: RegisteredLocale) => void;
3535
state: FirebaseUIState;
3636
setState: (state: FirebaseUIState) => void;
3737
locale: RegisteredLocale;
3838
behaviors: Partial<Record<BehaviorKey, BehaviorHandlers[BehaviorKey]>>;
39-
recaptchaMode: "normal" | "invisible";
4039
};
4140

4241
export const $config = map<Record<string, DeepMapStore<FirebaseUIConfiguration>>>({});
@@ -59,19 +58,18 @@ export function initializeUI(config: FirebaseUIConfigurationOptions, name: strin
5958
name,
6059
deepMap<FirebaseUIConfiguration>({
6160
app: config.app,
62-
getAuth: () => getAuth(config.app),
61+
auth: config.auth || getAuth(config.app),
6362
locale: config.locale ?? enUs,
6463
setLocale: (locale: RegisteredLocale) => {
6564
const current = $config.get()[name]!;
6665
current.setKey(`locale`, locale);
6766
},
68-
state: behaviors?.autoAnonymousLogin ? "signing-in" : "loading",
67+
state: behaviors?.autoAnonymousLogin ? "loading" : "idle",
6968
setState: (state: FirebaseUIState) => {
7069
const current = $config.get()[name]!;
7170
current.setKey(`state`, state);
7271
},
7372
behaviors: behaviors ?? {},
74-
recaptchaMode: config.recaptchaMode ?? "normal",
7573
})
7674
);
7775

packages/core/src/country-data.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
import { CountryData } from "./types";
16+
export interface CountryData {
17+
name: string;
18+
dialCode: string;
19+
code: string;
20+
emoji: string;
21+
};
1822

1923
export const countryData: CountryData[] = [
2024
{ name: "United States", dialCode: "+1", code: "US", emoji: "🇺🇸" },

packages/core/src/errors.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function handleFirebaseError(
3838
enableHandleExistingCredential?: boolean;
3939
}
4040
): never {
41+
// TODO(ehesp): Type error as unknown, check instance of FirebaseError
4142
if (error?.code === "auth/account-exists-with-different-credential") {
4243
if (opts?.enableHandleExistingCredential && error.credential) {
4344
window.sessionStorage.setItem("pendingCred", JSON.stringify(error.credential));

packages/core/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,5 @@ export * from "./behaviors";
1919
export * from "./config";
2020
export * from "./errors";
2121
export * from "./schemas";
22-
export * from "./types";
2322
export * from "./country-data";
2423
export * from "./translations";
25-
export type { CountryData } from "./types";

packages/core/src/state.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,4 @@
1414
* limitations under the License.
1515
*/
1616

17-
export type FirebaseUIState =
18-
| "loading"
19-
| "idle"
20-
| "signing-in"
21-
| "signing-out"
22-
| "linking"
23-
| "creating-user"
24-
| "sending-password-reset-email"
25-
| "sending-sign-in-link-to-email";
17+
export type FirebaseUIState = "idle" | "pending" | "loading";

packages/core/src/styles.css

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

packages/core/src/types.ts

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

0 commit comments

Comments
 (0)