ente_nazionale_frontend/app/login/page.tsx
2026-08-17 11:34:06 +03:00

124 lines
4.3 KiB
TypeScript

"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import Image from "next/image";
import { Loader2, Lock, Mail, TriangleAlert } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { login } from "@/lib/api";
export default function LoginPage() {
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 response = await login(email, password);
localStorage.setItem("user_role", response.user.role);
localStorage.setItem("user_email", response.user.email);
localStorage.setItem("association_id", response.association.id || "");
if (response.user.role === "CLIENT") {
router.push("/client-portal");
} else {
router.push("/dashboard");
}
} catch {
setError("Email o password non corretti");
} finally {
setLoading(false);
}
};
return (
<div className="relative flex min-h-screen flex-col overflow-hidden bg-muted">
{/* Tricolor accent bar */}
<div className="flex h-1.5 w-full shrink-0">
<span className="flex-1 bg-[#2e7d46]" />
<span className="flex-1 bg-foreground/10" />
<span className="flex-1 bg-[#b23b3b]" />
</div>
{/* Centered login card */}
<div className="relative z-10 flex flex-1 items-center justify-center p-6">
<Card className="w-full max-w-sm shadow-lg">
<CardHeader className="flex flex-col items-center gap-3 text-center">
<Image
src="/logo-encap.jpg"
alt="ENCAP"
width={44}
height={44}
className="rounded-md shadow-sm"
/>
<div>
<h2 className="text-xl font-semibold tracking-tight">Accedi</h2>
<p className="mt-1 text-sm text-muted-foreground">
Inserisci le tue credenziali per accedere al portale
</p>
</div>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-5">
{error && (
<div className="flex items-center gap-2 rounded-md bg-destructive/10 px-3 py-2.5 text-sm text-destructive">
<TriangleAlert className="size-4 shrink-0" />
{error}
</div>
)}
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<div className="relative">
<Mail className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
<Input
id="email"
type="email"
autoComplete="email"
placeholder="nome@associazione.it"
className="pl-9"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="password">Password</Label>
<div className="relative">
<Lock className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
<Input
id="password"
type="password"
autoComplete="current-password"
className="pl-9"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</div>
</div>
<Button type="submit" className="w-full" disabled={loading}>
{loading && <Loader2 className="size-4 animate-spin" />}
{loading ? "Accesso in corso..." : "Accedi"}
</Button>
</form>
</CardContent>
</Card>
</div>
</div>
);
}