Files
HoruxDespachosNuevo/apps/web/app/(auth)/login/page.tsx
Horux Dev 9f11a0ba39 feat: facturación primer pago, fixes SAT/MP, autocompletado RFCs/conceptos
Backend:
- Notificación email al admin cuando llega primer pago aprobado (sin factura auto)
- Endpoints GET /pagos-sin-factura y POST /emitir-factura-pago para admin global
- Fix vinculación org Facturapi Horux 360 (69f23a5a242e0af47a41fa0d)
- Fix webhook MP: validación defensiva de x-signature header
- Fix autocompleto RFCs: eliminado filtro por contribuyenteId
- Fix autocompleto conceptos: eliminado filtro por contribuyenteId
- SAT fixes: anti-bot CSF scraper, request reuse, date range fix, stale job thresholds
- SAT sync request reuse across jobs para evitar agotar cuota diaria
- Typo fix MP_ACCESS_TOKEN en .env
- Trial invitations system backend

Frontend:
- Nueva página /admin/facturas-pendientes con tabla y emisión manual
- Métrica 'Facturas pendientes' en /clientes (clickable)
- Navegación onboarding FIEL/CSD corregida
- Sidebar themes sincronizados
- Fix SAT portal migration scraper (NetIQ)
- Trial invitation acceptance pages
2026-05-09 21:56:42 +00:00

123 lines
4.4 KiB
TypeScript

'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<HTMLFormElement>) {
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 (
<div className="min-h-screen flex items-center justify-center p-4">
<Card className="w-full max-w-md">
<CardHeader className="text-center">
<div className="flex justify-center mb-4">
<Image
src="/logo.jpg"
alt="Horux360"
width={80}
height={80}
className="rounded-full"
priority
/>
</div>
<CardTitle className="text-2xl">Iniciar Sesión</CardTitle>
<CardDescription>
Ingresa tus credenciales para acceder a tu cuenta
</CardDescription>
</CardHeader>
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4">
{error && (
<div className="p-3 text-sm text-destructive bg-destructive/10 rounded-md">
{error}
</div>
)}
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
name="email"
type="email"
placeholder="tu@email.com"
required
/>
</div>
<div className="space-y-2">
<div className="flex items-center justify-between">
<Label htmlFor="password">Contraseña</Label>
<Link href="/forgot-password" className="text-xs text-primary hover:underline">
¿Olvidaste tu contraseña?
</Link>
</div>
<Input
id="password"
name="password"
type="password"
placeholder="••••••••"
required
/>
</div>
</CardContent>
<CardFooter className="flex flex-col gap-4">
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? 'Iniciando sesión...' : 'Iniciar Sesión'}
</Button>
<p className="text-sm text-muted-foreground text-center">
¿No tienes cuenta?{' '}
<Link href="/register-despacho" className="text-primary hover:underline">
Regístrate
</Link>
</p>
</CardFooter>
</form>
</Card>
</div>
);
}