'use client'; import { useState, Suspense } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { useLanguage } from '@/context/LanguageContext'; import { useAuth } from '@/context/AuthContext'; import Card from '@/components/ui/Card'; import Button from '@/components/ui/Button'; import Input from '@/components/ui/Input'; import GoogleSignInButton from '@/components/GoogleSignInButton'; import { authApi } from '@/lib/api'; import toast from 'react-hot-toast'; function LoginContent() { const router = useRouter(); const searchParams = useSearchParams(); const { t, locale: language } = useLanguage(); const { login } = useAuth(); const [loading, setLoading] = useState(false); const [loginMode, setLoginMode] = useState<'password' | 'magic-link'>('password'); const [magicLinkSent, setMagicLinkSent] = useState(false); const [formData, setFormData] = useState({ email: '', password: '', }); // Check for redirect after login const redirectTo = searchParams.get('redirect') || '/dashboard'; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await login(formData.email, formData.password); toast.success(language === 'es' ? '¡Bienvenido!' : 'Welcome back!'); router.push(redirectTo); } catch (error: any) { toast.error(error.message || t('auth.errors.invalidCredentials')); } finally { setLoading(false); } }; const handleMagicLinkRequest = async (e: React.FormEvent) => { e.preventDefault(); if (!formData.email) { toast.error(language === 'es' ? 'Ingresa tu email' : 'Please enter your email'); return; } setLoading(true); try { await authApi.requestMagicLink(formData.email); setMagicLinkSent(true); toast.success( language === 'es' ? 'Revisa tu correo para el enlace de acceso' : 'Check your email for the login link' ); } catch (error: any) { toast.error(error.message || (language === 'es' ? 'Error' : 'Failed')); } finally { setLoading(false); } }; return (

{t('auth.login.title')}

{t('auth.login.subtitle')}

{/* Google Sign-In Button */} {/* Or Divider */}
{language === 'es' ? 'o continuar con' : 'or continue with'}
{/* Login Mode Tabs */}
{loginMode === 'password' ? (
setFormData({ ...formData, email: e.target.value })} required /> setFormData({ ...formData, password: e.target.value })} required />
{language === 'es' ? '¿Olvidaste tu contraseña?' : 'Forgot password?'}
) : magicLinkSent ? (

{language === 'es' ? 'Revisa tu Email' : 'Check Your Email'}

{language === 'es' ? `Enviamos un enlace de acceso a ${formData.email}` : `We sent a login link to ${formData.email}`}

) : (
setFormData({ ...formData, email: e.target.value })} required />

{language === 'es' ? 'Te enviaremos un enlace para iniciar sesión sin contraseña' : "We'll send you a link to sign in without a password"}

)}

{t('auth.login.noAccount')}{' '} {t('auth.login.register')}

); } function LoadingFallback() { return (
); } export default function LoginPage() { return ( }> ); }