'use client'; import { useState, useEffect, useCallback } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import Link from 'next/link'; import { useRouter, useSearchParams } from 'next/navigation'; import { Eye, EyeOff, Mail, Lock, ArrowRight, Loader2, Languages } from 'lucide-react'; import { GoogleLogin } from '@react-oauth/google'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { useNotification } from '@/components/ui/notification'; import { useI18n } from '@/lib/i18n'; import { apiClient } from '@/lib/apiClient'; import { useLogin } from './useLogin'; import type { GoogleAuthResponse } from './types'; import { useGoogleConfig } from '@/providers/ClientGoogleProvider'; export function LoginForm() { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [googleLoading, setGoogleLoading] = useState(false); const loginMutation = useLogin(); const { notify } = useNotification(); const { t } = useI18n(); const router = useRouter(); const searchParams = useSearchParams(); const redirect = searchParams.get('redirect') || '/dashboard'; const { clientId: googleClientId, enabled: googleEnabled } = useGoogleConfig(); const queryClient = useQueryClient(); useEffect(() => { if (loginMutation.isError && loginMutation.error) { notify({ title: t('login.errorTitle'), description: loginMutation.error.message, variant: 'destructive', }); } }, [loginMutation.isError, loginMutation.error, notify, t]); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); loginMutation.mutate({ email, password }); }; const handleGoogleSuccess = useCallback(async (credentialResponse: { credential?: string }) => { if (!credentialResponse.credential) return; setGoogleLoading(true); try { const response = await apiClient.post<{ data: GoogleAuthResponse }>( '/api/v1/auth/google', { credential: credentialResponse.credential }, ); const { access_token, refresh_token } = response.data; queryClient.clear(); localStorage.setItem('token', access_token); localStorage.setItem('refresh_token', refresh_token); router.push(redirect); } catch { notify({ title: t('login.google.errorGeneric'), description: t('login.google.errorFailed'), variant: 'destructive', }); } finally { setGoogleLoading(false); } }, [redirect, router, notify, t, queryClient]); const handleGoogleError = useCallback(() => { notify({ title: t('login.google.errorGeneric'), description: t('login.google.errorFailed'), variant: 'destructive', }); }, [notify, t]); return (
{t('auth.brandName')} {t('login.welcomeBack')} {t('login.signInToContinue')}
{googleEnabled && googleClientId && ( <>
{googleLoading ? ( ) : ( )}
{t('login.orContinueWith')}
)}
setEmail(e.target.value)} leftIcon={} required />
{t('login.forgotPassword')}
setPassword(e.target.value)} leftIcon={} required />

{t('login.noAccount')}{' '} {t('login.signUpFree')}

); }