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 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
14526f92f2
commit
4b1408052a
@@ -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 (
|
||||||
|
<div className="min-h-screen flex items-center justify-center bg-background px-4">
|
||||||
|
<div className="w-full max-w-sm">
|
||||||
|
<div className="bg-white rounded-xl shadow-[0_8px_30px_-12px_rgba(15,23,42,0.18)] p-8">
|
||||||
|
<h1 className="text-2xl font-bold tracking-tight text-center mb-1">
|
||||||
|
{esCliente ? "Portal de clientes" : "Cotizador"}
|
||||||
|
</h1>
|
||||||
|
<p className="text-center text-muted text-sm mb-6">
|
||||||
|
{esCliente
|
||||||
|
? "Consulta tus cotizaciones y registra tus pagos"
|
||||||
|
: "Uriel Jareth Consulting"}
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<div role="alert" className="bg-red-50 border border-red-200 text-red-700 text-sm rounded-lg px-4 py-3 mb-4">
|
||||||
|
{error}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<label htmlFor="login-email" className="block text-sm font-medium mb-1">
|
||||||
|
Correo
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="login-email"
|
||||||
|
type="email"
|
||||||
|
autoComplete="email"
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
required
|
||||||
|
placeholder="[email protected]"
|
||||||
|
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"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="login-password" className="block text-sm font-medium mb-1">
|
||||||
|
Contraseña
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="login-password"
|
||||||
|
type="password"
|
||||||
|
autoComplete="current-password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => 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"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
disabled={loading}
|
||||||
|
suppressHydrationWarning
|
||||||
|
className="w-full py-2.5 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary-dark disabled:opacity-50 transition-colors"
|
||||||
|
>
|
||||||
|
{loading ? "Ingresando..." : "Ingresar"}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
+8
-118
@@ -1,120 +1,10 @@
|
|||||||
"use client";
|
import { LoginForm } from "./LoginForm";
|
||||||
|
|
||||||
import { Suspense, useState } from "react";
|
export default async function LoginPage({
|
||||||
import { useRouter, useSearchParams } from "next/navigation";
|
searchParams,
|
||||||
|
}: {
|
||||||
// useSearchParams exige un limite de Suspense para que /login siga siendo
|
searchParams: Promise<{ panel?: string }>;
|
||||||
// estatica en el build.
|
}) {
|
||||||
export default function LoginPage() {
|
const { panel } = await searchParams;
|
||||||
return (
|
return <LoginForm esCliente={panel === "cliente"} />;
|
||||||
<Suspense>
|
|
||||||
<LoginForm />
|
|
||||||
</Suspense>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (
|
|
||||||
<div className="min-h-screen flex items-center justify-center bg-background px-4">
|
|
||||||
<div className="w-full max-w-sm">
|
|
||||||
<div className="bg-white rounded-xl shadow-[0_8px_30px_-12px_rgba(15,23,42,0.18)] p-8">
|
|
||||||
<h1 className="text-2xl font-bold tracking-tight text-center mb-1">
|
|
||||||
{esCliente ? "Portal de clientes" : "Cotizador"}
|
|
||||||
</h1>
|
|
||||||
<p className="text-center text-muted text-sm mb-6">
|
|
||||||
{esCliente
|
|
||||||
? "Consulta tus cotizaciones y registra tus pagos"
|
|
||||||
: "Uriel Jareth Consulting"}
|
|
||||||
</p>
|
|
||||||
|
|
||||||
{error && (
|
|
||||||
<div role="alert" className="bg-red-50 border border-red-200 text-red-700 text-sm rounded-lg px-4 py-3 mb-4">
|
|
||||||
{error}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<form onSubmit={handleSubmit} className="space-y-4">
|
|
||||||
<div>
|
|
||||||
<label htmlFor="login-email" className="block text-sm font-medium mb-1">
|
|
||||||
Correo
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
id="login-email"
|
|
||||||
type="email"
|
|
||||||
autoComplete="email"
|
|
||||||
value={email}
|
|
||||||
onChange={(e) => setEmail(e.target.value)}
|
|
||||||
required
|
|
||||||
placeholder="[email protected]"
|
|
||||||
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"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div>
|
|
||||||
<label htmlFor="login-password" className="block text-sm font-medium mb-1">
|
|
||||||
Contraseña
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
id="login-password"
|
|
||||||
type="password"
|
|
||||||
autoComplete="current-password"
|
|
||||||
value={password}
|
|
||||||
onChange={(e) => 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"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<button
|
|
||||||
type="submit"
|
|
||||||
disabled={loading}
|
|
||||||
suppressHydrationWarning
|
|
||||||
className="w-full py-2.5 bg-primary text-white rounded-lg text-sm font-medium hover:bg-primary-dark disabled:opacity-50 transition-colors"
|
|
||||||
>
|
|
||||||
{loading ? "Ingresando..." : "Ingresar"}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user