From 4b1408052a4317947666a266d8cdca0526adb9a6 Mon Sep 17 00:00:00 2001 From: urieljareth Date: Mon, 26 Oct 2026 15:59:01 -0600 Subject: [PATCH] Login: renderizar el formulario en el servidor (sin Suspense) useSearchParams + Suspense sacaba el formulario completo del HTML: /login quedaba en blanco hasta cargar el JS. page.tsx lee ?panel en el servidor y pasa esCliente a LoginForm, asi el HTML ya trae el formulario. Co-Authored-By: Claude Opus 5.5 --- src/app/login/LoginForm.tsx | 111 +++++++++++++++++++++++++++++++ src/app/login/page.tsx | 126 +++--------------------------------- 2 files changed, 119 insertions(+), 118 deletions(-) create mode 100644 src/app/login/LoginForm.tsx diff --git a/src/app/login/LoginForm.tsx b/src/app/login/LoginForm.tsx new file mode 100644 index 0000000..757eeee --- /dev/null +++ b/src/app/login/LoginForm.tsx @@ -0,0 +1,111 @@ +"use client"; + +import { useState } from "react"; +import { useRouter } from "next/navigation"; + +// /login?panel=cliente: el middleware manda aqui a quien abre /cliente sin +// sesion; el formulario es el mismo, solo cambia el encabezado. `esCliente` lo +// resuelve page.tsx en el servidor: con useSearchParams + Suspense el +// formulario completo salia del HTML y aparecia hasta cargar el JS. +export function LoginForm({ esCliente }: { esCliente: boolean }) { + const router = useRouter(); + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + setError(""); + setLoading(true); + + try { + const res = await fetch("/api/auth/login", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ email, password }), + }); + + if (res.ok) { + const data = await res.json(); + // Cada rol entra a su propio panel. + const destino = data?.user?.role === "cliente" ? "/cliente" : "/"; + router.push(destino); + router.refresh(); + } else { + const data = await res.json(); + setError(data.error || "No se pudo iniciar sesión"); + } + } catch { + setError("Sin conexión con el servidor. Intenta de nuevo."); + } finally { + setLoading(false); + } + }; + + return ( +
+
+
+

+ {esCliente ? "Portal de clientes" : "Cotizador"} +

+

+ {esCliente + ? "Consulta tus cotizaciones y registra tus pagos" + : "Uriel Jareth Consulting"} +

+ + {error && ( +
+ {error} +
+ )} + +
+
+ + setEmail(e.target.value)} + required + placeholder="tu@email.com" + suppressHydrationWarning + className="w-full px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary" + /> +
+
+ + setPassword(e.target.value)} + required + placeholder="Tu contraseña" + suppressHydrationWarning + className="w-full px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary" + /> +
+ +
+
+
+
+ ); +} diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 07a7d2b..f08c1e6 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -1,120 +1,10 @@ -"use client"; +import { LoginForm } from "./LoginForm"; -import { Suspense, useState } from "react"; -import { useRouter, useSearchParams } from "next/navigation"; - -// useSearchParams exige un limite de Suspense para que /login siga siendo -// estatica en el build. -export default function LoginPage() { - return ( - - - - ); -} - -function LoginForm() { - const router = useRouter(); - // /login?panel=cliente: el middleware manda aqui a quien abre /cliente sin - // sesion; el formulario es el mismo, solo cambia el encabezado. - const esCliente = useSearchParams().get("panel") === "cliente"; - const [email, setEmail] = useState(""); - const [password, setPassword] = useState(""); - const [error, setError] = useState(""); - const [loading, setLoading] = useState(false); - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - setError(""); - setLoading(true); - - try { - const res = await fetch("/api/auth/login", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ email, password }), - }); - - if (res.ok) { - const data = await res.json(); - // Cada rol entra a su propio panel. - const destino = data?.user?.role === "cliente" ? "/cliente" : "/"; - router.push(destino); - router.refresh(); - } else { - const data = await res.json(); - setError(data.error || "No se pudo iniciar sesión"); - } - } catch { - setError("Sin conexión con el servidor. Intenta de nuevo."); - } finally { - setLoading(false); - } - }; - - return ( -
-
-
-

- {esCliente ? "Portal de clientes" : "Cotizador"} -

-

- {esCliente - ? "Consulta tus cotizaciones y registra tus pagos" - : "Uriel Jareth Consulting"} -

- - {error && ( -
- {error} -
- )} - -
-
- - setEmail(e.target.value)} - required - placeholder="tu@email.com" - suppressHydrationWarning - className="w-full px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary" - /> -
-
- - setPassword(e.target.value)} - required - placeholder="Tu contraseña" - suppressHydrationWarning - className="w-full px-3 py-2 border border-border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-primary/20 focus:border-primary" - /> -
- -
-
-
-
- ); +export default async function LoginPage({ + searchParams, +}: { + searchParams: Promise<{ panel?: string }>; +}) { + const { panel } = await searchParams; + return ; }