'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Lock, User, Key, ArrowRight, ShieldCheck, Loader2 } from 'lucide-react'; import Link from 'next/link'; export default function LoginPage() { const router = useRouter(); const [step, setStep] = useState<'credentials' | 'otp'>('credentials'); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [formData, setFormData] = useState({ username: '', password: '', otp: '' }); const handleLogin = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { const res = await fetch('/api/auth/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: formData.username, password: formData.password }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Error al iniciar sesión'); if (data.requireOtp) { setStep('otp'); } else { router.push('/'); router.refresh(); } } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; const handleVerifyOtp = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); try { const res = await fetch('/api/auth/verify-otp', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: formData.username, otp: formData.otp }) }); const data = await res.json(); if (!res.ok) throw new Error(data.error || 'Código incorrecto'); router.push('/'); router.refresh(); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; return (
Sistema de Finanzas Personales
¿No tienes cuenta?{' '} Regístrate aquí