'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import Link from 'next/link'; import Image from 'next/image'; import { Button, Input, Label, Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@horux/shared-ui'; import { login } from '@/lib/api/auth'; import { useAuthStore } from '@/stores/auth-store'; import { isGlobalAdminRfc, type PlatformRole } from '@horux/shared'; import { shouldShowOnboarding } from '@/lib/onboarding'; export default function LoginPage() { const router = useRouter(); const { setUser, setTokens } = useAuthStore(); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setIsLoading(true); setError(''); const formData = new FormData(e.currentTarget); const email = formData.get('email') as string; const password = formData.get('password') as string; try { const response = await login({ email, password }); setTokens(response.accessToken, response.refreshToken); setUser(response.user); const userRole = response.user?.role; // Admin global aterriza directo en `/clientes` — su home natural es la // gestión de tenants, no el dashboard operativo del despacho. const platformRoles = (response.user as { platformRoles?: PlatformRole[] }).platformRoles; const isGlobalAdmin = isGlobalAdminRfc(response.user?.tenantRfc, userRole, platformRoles); if (isGlobalAdmin) { router.push('/clientes'); } else if (userRole === 'cliente' || userRole === 'auxiliar' || userRole === 'supervisor') { // Clients and roles without onboarding go straight to dashboard router.push('/dashboard'); } else { // Owner/CFO/Contador: onboarding hasta 4 logins o hasta que el user // complete los pasos requeridos (lo que pase primero). router.push(shouldShowOnboarding(response.user) ? '/onboarding' : '/dashboard'); } } catch (err: any) { setError(err.response?.data?.message || 'Error al iniciar sesión'); } finally { setIsLoading(false); } } return (
Horux360
Iniciar Sesión Ingresa tus credenciales para acceder a tu cuenta
{error && (
{error}
)}
¿Olvidaste tu contraseña?

¿No tienes cuenta?{' '} Regístrate

); }