Add Facebook social login
This commit is contained in:
@@ -3,7 +3,7 @@ import { usePage } from "@inertiajs/react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
||||
import { useCheckoutWizard } from "../WizardContext";
|
||||
import type { GoogleProfilePrefill } from '../types';
|
||||
import type { OAuthProfilePrefill } from '../types';
|
||||
import LoginForm, { AuthUserPayload } from "../../../auth/LoginForm";
|
||||
import RegisterForm, { RegisterSuccessPayload } from "../../../auth/RegisterForm";
|
||||
import { Trans, useTranslation } from 'react-i18next';
|
||||
@@ -14,11 +14,11 @@ import { cn } from "@/lib/utils";
|
||||
|
||||
interface AuthStepProps {
|
||||
privacyHtml: string;
|
||||
googleProfile?: GoogleProfilePrefill;
|
||||
onClearGoogleProfile?: () => void;
|
||||
prefill?: OAuthProfilePrefill;
|
||||
onClearPrefill?: () => void;
|
||||
}
|
||||
|
||||
type GoogleAuthFlash = {
|
||||
type OAuthAuthFlash = {
|
||||
status?: string | null;
|
||||
error?: string | null;
|
||||
};
|
||||
@@ -34,26 +34,55 @@ const GoogleIcon: React.FC<{ className?: string }> = ({ className }) => (
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile, onClearGoogleProfile }) => {
|
||||
const FacebookIcon: React.FC<{ className?: string }> = ({ className }) => (
|
||||
<svg
|
||||
className={className}
|
||||
viewBox="0 0 24 24"
|
||||
aria-hidden="true"
|
||||
focusable="false"
|
||||
>
|
||||
<path
|
||||
fill="#1877F2"
|
||||
d="M24 12.073C24 5.404 18.627 0 12 0S0 5.404 0 12.073C0 18.1 4.388 23.094 10.125 24v-8.437H7.078v-3.49h3.047V9.43c0-3.014 1.792-4.68 4.533-4.68 1.312 0 2.686.236 2.686.236v2.96h-1.513c-1.49 0-1.954.93-1.954 1.887v2.264h3.328l-.532 3.49h-2.796V24C19.612 23.094 24 18.1 24 12.073Z"
|
||||
/>
|
||||
</svg>
|
||||
);
|
||||
|
||||
export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, prefill, onClearPrefill }) => {
|
||||
const { t } = useTranslation('marketing');
|
||||
const page = usePage<{ locale?: string }>();
|
||||
const locale = page.props.locale ?? "de";
|
||||
const googleAuth = useMemo<GoogleAuthFlash>(() => {
|
||||
const props = page.props as { googleAuth?: GoogleAuthFlash };
|
||||
const googleAuth = useMemo<OAuthAuthFlash>(() => {
|
||||
const props = page.props as { googleAuth?: OAuthAuthFlash };
|
||||
return props.googleAuth ?? {};
|
||||
}, [page.props]);
|
||||
const facebookAuth = useMemo<OAuthAuthFlash>(() => {
|
||||
const props = page.props as { facebookAuth?: OAuthAuthFlash };
|
||||
return props.facebookAuth ?? {};
|
||||
}, [page.props]);
|
||||
const { isAuthenticated, authUser, setAuthUser, nextStep, selectedPackage } = useCheckoutWizard();
|
||||
const [mode, setMode] = useState<'login' | 'register'>('register');
|
||||
const [isRedirectingToGoogle, setIsRedirectingToGoogle] = useState(false);
|
||||
const [isRedirectingToFacebook, setIsRedirectingToFacebook] = useState(false);
|
||||
const [showGoogleHelper, setShowGoogleHelper] = useState(false);
|
||||
const showGoogleControls = mode === 'register';
|
||||
const showOauthControls = mode === 'register';
|
||||
const authErrorTitle = facebookAuth?.error
|
||||
? t('checkout.auth_step.facebook_error_title')
|
||||
: t('checkout.auth_step.google_error_title');
|
||||
|
||||
useEffect(() => {
|
||||
if (googleAuth?.status === 'signin') {
|
||||
toast.success(t('checkout.auth_step.google_success_toast'));
|
||||
onClearGoogleProfile?.();
|
||||
onClearPrefill?.();
|
||||
}
|
||||
}, [googleAuth?.status, onClearGoogleProfile, t]);
|
||||
}, [googleAuth?.status, onClearPrefill, t]);
|
||||
|
||||
useEffect(() => {
|
||||
if (facebookAuth?.status === 'signin') {
|
||||
toast.success(t('checkout.auth_step.facebook_success_toast'));
|
||||
onClearPrefill?.();
|
||||
}
|
||||
}, [facebookAuth?.status, onClearPrefill, t]);
|
||||
|
||||
useEffect(() => {
|
||||
if (googleAuth?.error) {
|
||||
@@ -61,6 +90,12 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
}
|
||||
}, [googleAuth?.error]);
|
||||
|
||||
useEffect(() => {
|
||||
if (facebookAuth?.error) {
|
||||
toast.error(facebookAuth.error);
|
||||
}
|
||||
}, [facebookAuth?.error]);
|
||||
|
||||
useEffect(() => {
|
||||
if (mode !== 'login') {
|
||||
return;
|
||||
@@ -82,7 +117,7 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
name: payload.name ?? undefined,
|
||||
pending_purchase: Boolean(payload.pending_purchase),
|
||||
});
|
||||
onClearGoogleProfile?.();
|
||||
onClearPrefill?.();
|
||||
nextStep();
|
||||
};
|
||||
|
||||
@@ -97,7 +132,7 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
});
|
||||
}
|
||||
|
||||
onClearGoogleProfile?.();
|
||||
onClearPrefill?.();
|
||||
nextStep();
|
||||
};
|
||||
|
||||
@@ -115,6 +150,20 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
window.location.href = `/checkout/auth/google?${params.toString()}`;
|
||||
}, [locale, selectedPackage, t]);
|
||||
|
||||
const handleFacebookLogin = useCallback(() => {
|
||||
if (!selectedPackage) {
|
||||
toast.error(t('checkout.auth_step.facebook_missing_package'));
|
||||
return;
|
||||
}
|
||||
|
||||
setIsRedirectingToFacebook(true);
|
||||
const params = new URLSearchParams({
|
||||
package_id: String(selectedPackage.id),
|
||||
locale,
|
||||
});
|
||||
window.location.href = `/checkout/auth/facebook?${params.toString()}`;
|
||||
}, [locale, selectedPackage, t]);
|
||||
|
||||
if (isAuthenticated && authUser) {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
@@ -159,37 +208,52 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
>
|
||||
{t('checkout.auth_step.switch_to_login')}
|
||||
</Button>
|
||||
{showGoogleControls && (
|
||||
<div className="flex flex-1 justify-start gap-2 sm:flex-none">
|
||||
{showOauthControls && (
|
||||
<div className="flex flex-1 flex-wrap items-center gap-2 sm:flex-none">
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleGoogleLogin}
|
||||
disabled={isRedirectingToGoogle}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
{isRedirectingToGoogle ? (
|
||||
<LoaderCircle className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<GoogleIcon className="h-4 w-4" />
|
||||
)}
|
||||
{t('checkout.auth_step.continue_with_google')}
|
||||
</Button>
|
||||
<CollapsibleTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 rounded-full border border-muted-foreground/30 px-3 py-1 text-xs font-medium text-muted-foreground transition hover:border-muted-foreground/60 hover:text-foreground",
|
||||
showGoogleHelper && "bg-muted/60 text-foreground"
|
||||
)}
|
||||
>
|
||||
{t('checkout.auth_step.google_helper_badge')}
|
||||
<ChevronDown className={cn("h-3 w-3 transition-transform", showGoogleHelper && "rotate-180")} />
|
||||
</button>
|
||||
</CollapsibleTrigger>
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={handleGoogleLogin}
|
||||
disabled={isRedirectingToGoogle}
|
||||
onClick={handleFacebookLogin}
|
||||
disabled={isRedirectingToFacebook}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
{isRedirectingToGoogle ? (
|
||||
{isRedirectingToFacebook ? (
|
||||
<LoaderCircle className="h-4 w-4 animate-spin" />
|
||||
) : (
|
||||
<GoogleIcon className="h-4 w-4" />
|
||||
<FacebookIcon className="h-4 w-4" />
|
||||
)}
|
||||
{t('checkout.auth_step.continue_with_google')}
|
||||
{t('checkout.auth_step.continue_with_facebook')}
|
||||
</Button>
|
||||
<CollapsibleTrigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1 rounded-full border border-muted-foreground/30 px-3 py-1 text-xs font-medium text-muted-foreground transition hover:border-muted-foreground/60 hover:text-foreground",
|
||||
showGoogleHelper && "bg-muted/60 text-foreground"
|
||||
)}
|
||||
>
|
||||
{t('checkout.auth_step.google_helper_badge')}
|
||||
<ChevronDown className={cn("h-3 w-3 transition-transform", showGoogleHelper && "rotate-180")} />
|
||||
</button>
|
||||
</CollapsibleTrigger>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
{showGoogleControls && (
|
||||
{showOauthControls && (
|
||||
<CollapsibleContent>
|
||||
<Alert className="border-dashed border-muted/60 bg-muted/20 text-xs sm:text-sm">
|
||||
<AlertDescription>{t('checkout.auth_step.google_helper')}</AlertDescription>
|
||||
@@ -198,10 +262,10 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
)}
|
||||
</Collapsible>
|
||||
|
||||
{googleAuth?.error && (
|
||||
{(googleAuth?.error || facebookAuth?.error) && (
|
||||
<Alert variant="destructive">
|
||||
<AlertTitle>{t('checkout.auth_step.google_error_title')}</AlertTitle>
|
||||
<AlertDescription>{googleAuth.error}</AlertDescription>
|
||||
<AlertTitle>{authErrorTitle}</AlertTitle>
|
||||
<AlertDescription>{facebookAuth?.error ?? googleAuth?.error}</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
@@ -213,8 +277,8 @@ export const AuthStep: React.FC<AuthStepProps> = ({ privacyHtml, googleProfile,
|
||||
privacyHtml={privacyHtml}
|
||||
locale={locale}
|
||||
onSuccess={handleRegisterSuccess}
|
||||
prefill={googleProfile}
|
||||
onClearGoogleProfile={onClearGoogleProfile}
|
||||
prefill={prefill}
|
||||
onClearPrefill={onClearPrefill}
|
||||
/>
|
||||
)
|
||||
) : (
|
||||
|
||||
Reference in New Issue
Block a user