Skip to content

Commit 582f192

Browse files
committed
feat: Add support for existing credentials in the react package
1 parent dcbefb2 commit 582f192

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

packages/firebaseui-react/src/auth/forms/email-link-form.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import { TermsAndPrivacy } from "../../components/terms-and-privacy";
1616

1717
export function EmailLinkForm() {
1818
const auth = useAuth();
19-
const { language, enableAutoUpgradeAnonymous } = useConfig();
19+
const {
20+
language,
21+
enableAutoUpgradeAnonymous,
22+
enableHandleExistingCredential,
23+
} = useConfig();
2024
const translations = useTranslations();
2125
const [formError, setFormError] = useState<string | null>(null);
2226
const [emailSent, setEmailSent] = useState(false);
@@ -65,6 +69,7 @@ export function EmailLinkForm() {
6569
translations,
6670
language,
6771
enableAutoUpgradeAnonymous,
72+
enableHandleExistingCredential,
6873
});
6974
} catch (error) {
7075
if (error instanceof FirebaseUIError) {
@@ -74,7 +79,13 @@ export function EmailLinkForm() {
7479
};
7580

7681
void completeSignIn();
77-
}, [auth, translations, language, enableAutoUpgradeAnonymous]);
82+
}, [
83+
auth,
84+
translations,
85+
language,
86+
enableAutoUpgradeAnonymous,
87+
enableHandleExistingCredential,
88+
]);
7889

7990
if (emailSent) {
8091
// TODO: Improve this UI

packages/firebaseui-react/src/auth/forms/email-password-form.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export function EmailPasswordForm({
2525
}: EmailPasswordFormProps) {
2626
const auth = useAuth();
2727
const translations = useTranslations();
28-
const { language, enableAutoUpgradeAnonymous } = useConfig();
28+
const {
29+
language,
30+
enableAutoUpgradeAnonymous,
31+
enableHandleExistingCredential,
32+
} = useConfig();
2933
const [formError, setFormError] = useState<string | null>(null);
3034

3135
// TODO: Do we need to memoize this?
@@ -50,6 +54,7 @@ export function EmailPasswordForm({
5054
translations,
5155
language,
5256
enableAutoUpgradeAnonymous,
57+
enableHandleExistingCredential,
5358
});
5459
} catch (error) {
5560
if (error instanceof FirebaseUIError) {

packages/firebaseui-react/src/auth/forms/phone-form.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ export interface PhoneFormProps {
298298

299299
export function PhoneForm({ resendDelay = 30 }: PhoneFormProps) {
300300
const auth = useAuth();
301-
const { language, recaptchaMode } = useConfig();
301+
const {
302+
language,
303+
recaptchaMode,
304+
enableAutoUpgradeAnonymous,
305+
enableHandleExistingCredential,
306+
} = useConfig();
302307
const translations = useTranslations();
303308
const [formError, setFormError] = useState<string | null>(null);
304309
const [confirmationResult, setConfirmationResult] =
@@ -423,6 +428,8 @@ export function PhoneForm({ resendDelay = 30 }: PhoneFormProps) {
423428
await fuiConfirmPhoneNumber(confirmationResult, code, {
424429
translations,
425430
language,
431+
enableAutoUpgradeAnonymous,
432+
enableHandleExistingCredential,
426433
});
427434
} catch (error) {
428435
if (error instanceof FirebaseUIError) {
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"use client";
22

3-
import { FirebaseUIError, fuiSignInWithOAuth } from "@firebase-ui/core";
3+
import {
4+
FirebaseUIError,
5+
fuiSignInWithOAuth,
6+
getTranslation,
7+
} from "@firebase-ui/core";
48
import type { AuthProvider } from "firebase/auth";
59
import type { PropsWithChildren } from "react";
610
import { Button } from "~/components/button";
711
import { useAuth, useConfig, useTranslations } from "~/hooks";
12+
import { useState } from "react";
813

914
export type OAuthButtonProps = PropsWithChildren<{
1015
provider: AuthProvider;
@@ -13,29 +18,44 @@ export type OAuthButtonProps = PropsWithChildren<{
1318
export function OAuthButton({ provider, children }: OAuthButtonProps) {
1419
const auth = useAuth();
1520
const translations = useTranslations();
16-
const { language, enableAutoUpgradeAnonymous } = useConfig();
21+
const {
22+
language,
23+
enableAutoUpgradeAnonymous,
24+
enableHandleExistingCredential,
25+
} = useConfig();
26+
const [error, setError] = useState<string | null>(null);
1727

1828
const handleOAuthSignIn = async () => {
29+
setError(null);
1930
try {
2031
await fuiSignInWithOAuth(auth, provider, {
2132
translations,
2233
language,
2334
enableAutoUpgradeAnonymous,
35+
enableHandleExistingCredential,
2436
});
2537
} catch (error) {
2638
if (error instanceof FirebaseUIError) {
27-
// TODO: What happens here?
39+
setError(error.message);
40+
return;
2841
}
42+
console.error(error);
43+
setError(
44+
getTranslation("errors", "unknownError", translations, language)
45+
);
2946
}
3047
};
3148

3249
return (
33-
<Button
34-
type="button"
35-
onClick={handleOAuthSignIn}
36-
className="fui-provider__button"
37-
>
38-
{children}
39-
</Button>
50+
<div>
51+
<Button
52+
type="button"
53+
onClick={handleOAuthSignIn}
54+
className="fui-provider__button"
55+
>
56+
{children}
57+
</Button>
58+
{error && <div className="fui-form__error">{error}</div>}
59+
</div>
4060
);
4161
}

0 commit comments

Comments
 (0)