trello_fake/app/login/page.tsx

178 lines
5.7 KiB
TypeScript
Raw Normal View History

"use client";
import { useState } from "react";
import { useAuth } from "@/hooks/use-auth";
import { FolderKanban, Loader2, Eye, EyeOff } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Alert, AlertDescription } from "@/components/ui/alert";
export default function LoginPage() {
const { login, isLoading: authLoading } = useAuth();
const [formData, setFormData] = useState({
login: "",
password: "",
});
const [showPassword, setShowPassword] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { name, value } = e.target;
setFormData((prev) => ({ ...prev, [name]: value }));
// Limpiar error al escribir
if (error) setError(null);
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError(null);
setIsSubmitting(true);
// Validación básica
if (!formData.login.trim()) {
setError("El usuario es requerido");
setIsSubmitting(false);
return;
}
if (!formData.password) {
setError("La contraseña es requerida");
setIsSubmitting(false);
return;
}
try {
const result = await login({
login: formData.login.trim(),
password: formData.password,
});
if (!result.success) {
setError(result.error || "Error al iniciar sesión");
}
// Si success, el AuthProvider redirigirá automáticamente
} catch {
setError("Error inesperado. Por favor, intenta de nuevo.");
} finally {
setIsSubmitting(false);
}
};
const isLoading = isSubmitting || authLoading;
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-slate-50 to-slate-100 dark:from-slate-950 dark:to-slate-900 p-4">
<Card className="w-full max-w-md shadow-xl border-0 bg-card/80 backdrop-blur">
{/* Header */}
<CardHeader className="space-y-4 text-center pb-2">
<div className="mx-auto flex h-14 w-14 items-center justify-center rounded-xl bg-gradient-to-br from-blue-500 to-purple-600 shadow-lg">
<FolderKanban className="h-7 w-7 text-white" />
</div>
<div className="space-y-1">
<CardTitle className="text-2xl font-bold tracking-tight">
Dolibarr Proyectos
</CardTitle>
<CardDescription className="text-muted-foreground">
Inicia sesión para acceder al panel de gestión
</CardDescription>
</div>
</CardHeader>
{/* Form */}
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4 pt-4">
{/* Error Alert */}
{error && (
<Alert variant="destructive" className="animate-in fade-in-50">
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
{/* Usuario */}
<div className="space-y-2">
<Label htmlFor="login">Usuario</Label>
<Input
id="login"
name="login"
type="text"
placeholder="Tu nombre de usuario"
value={formData.login}
onChange={handleChange}
disabled={isLoading}
autoComplete="username"
autoFocus
className="h-11"
/>
</div>
{/* Contraseña */}
<div className="space-y-2">
<Label htmlFor="password">Contraseña</Label>
<div className="relative">
<Input
id="password"
name="password"
type={showPassword ? "text" : "password"}
placeholder="Tu contraseña"
value={formData.password}
onChange={handleChange}
disabled={isLoading}
autoComplete="current-password"
className="h-11 pr-10"
/>
<Button
type="button"
variant="ghost"
size="icon"
className="absolute right-0 top-0 h-11 w-11 hover:bg-transparent"
onClick={() => setShowPassword(!showPassword)}
disabled={isLoading}
tabIndex={-1}
>
{showPassword ? (
<EyeOff className="h-4 w-4 text-muted-foreground" />
) : (
<Eye className="h-4 w-4 text-muted-foreground" />
)}
</Button>
</div>
</div>
</CardContent>
<CardFooter className="flex flex-col gap-4 pt-2">
<Button
type="submit"
className="w-full h-11 bg-gradient-to-r from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 text-white font-medium"
disabled={isLoading}
>
{isLoading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Iniciando sesión...
</>
) : (
"Iniciar sesión"
)}
</Button>
<p className="text-xs text-center text-muted-foreground">
Usa tus credenciales de Dolibarr para acceder
</p>
</CardFooter>
</form>
</Card>
</div>
);
}