'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.subtitle')}
{language === 'es' ? `Enviamos un enlace de acceso a ${formData.email}` : `We sent a login link to ${formData.email}`}
{t('auth.login.noAccount')}{' '} {t('auth.login.register')}