import { useState, useEffect } from "react"; import { useNavigate, Link } from "react-router-dom"; import { useAuth } from "@/contexts/AuthContext"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage } from "@/components/ui/form"; import { z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { useToast } from "@/hooks/use-toast"; import { useTranslation } from "@/hooks/useTranslation"; import { Loader2 } from "lucide-react"; declare global { interface Window { handleSignInWithGoogle: (response: { credential: string }) => Promise; google?: { accounts: { id: { initialize: (config: { client_id: string; callback: (response: { credential: string }) => Promise; }) => void; prompt: () => void; }; }; }; } } const formSchema = z.object({ email: z.string().email(), password: z.string().min(6), }); export const Login = () => { const { signIn, signInWithGoogle, signInWithGitHub, user } = useAuth(); const navigate = useNavigate(); const [isLoading, setIsLoading] = useState(false); const [isOAuthLoading, setIsOAuthLoading] = useState(false); const { toast } = useToast(); const t = useTranslation(); const isEmbedded = !!import.meta.env.VITE_HUGGINGFACE; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { email: "", password: "", }, }); useEffect(() => { // Add Google Sign-In script const script = document.createElement('script'); script.src = 'https://accounts.google.com/gsi/client'; script.async = true; script.onload = () => { // Initialize Google Identity Services window.google?.accounts.id.initialize({ client_id: import.meta.env.VITE_GOOGLE_CLIENT_ID, callback: window.handleSignInWithGoogle, }); }; document.body.appendChild(script); return () => { document.body.removeChild(script); }; }, []); useEffect(() => { if (user && isOAuthLoading) { setIsOAuthLoading(false); toast({ title: t.auth.loginSuccess.title, description: t.auth.loginSuccess.description, }); navigate("/"); } }, [user, isOAuthLoading, navigate, toast, t.auth.loginSuccess]); useEffect(() => { // Initialize Google One Tap window.handleSignInWithGoogle = async (response) => { try { if (!response.credential) { throw new Error('No credential received from Google'); } setIsOAuthLoading(true); const { error } = await signInWithGoogle(); if (error) { setIsOAuthLoading(false); toast({ variant: "destructive", title: t.auth.loginError.title, description: error.message, }); } } catch (error) { setIsOAuthLoading(false); toast({ variant: "destructive", title: t.auth.loginError.title, description: t.auth.loginError.description, }); } }; }, [signInWithGoogle, toast, t.auth.loginError]); const handleGitHubLogin = async () => { try { setIsOAuthLoading(true); const { error } = await signInWithGitHub(); if (error) { setIsOAuthLoading(false); toast({ variant: "destructive", title: t.auth.loginError.title, description: error.message, }); } } catch (error) { setIsOAuthLoading(false); toast({ variant: "destructive", title: t.auth.loginError.title, description: t.auth.loginError.description, }); } }; const onSubmit = async (values: z.infer) => { try { setIsLoading(true); const { error, success } = await signIn(values.email, values.password); if (success) { toast({ title: t.auth.loginSuccess.title, description: t.auth.loginSuccess.description, }); navigate("/"); } else if (error) { toast({ variant: "destructive", title: t.auth.loginError.title, description: error.message, }); } } catch (error) { toast({ variant: "destructive", title: t.auth.loginError.title, description: t.auth.loginError.description, }); } finally { setIsLoading(false); } }; return (
{isOAuthLoading ? (

Authenticating...

) : (

{t.auth.login.title}

{t.auth.login.subtitle}

{isEmbedded && (

Social logins are only available on{" "} think-in-sync.com

)}
Or continue with email
( {t.auth.form.email} )} /> ( {t.auth.form.password} )} />

{t.auth.login.noAccount}{" "} {t.auth.register.linkText}

)}
); }; export default Login;