82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
"use client";
|
|
|
|
import Sidebar from "@/components/admin/Sidebar";
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
import { useRouter } from "next/navigation";
|
|
import { useEffect } from "react";
|
|
import { Loading } from "@/components/ui/Loading";
|
|
import toast from "react-hot-toast";
|
|
import { LogOut } from "lucide-react";
|
|
import Logo from "@/components/Logo";
|
|
|
|
export default function AdminLayout({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const { user, isAuthenticated, isLoading, logout } = useAuth();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
if (!isLoading && !isAuthenticated) {
|
|
router.push("/login");
|
|
return;
|
|
}
|
|
|
|
if (isAuthenticated && user?.role !== "ADMIN" && user?.role !== "admin") {
|
|
router.push("/");
|
|
toast.error("Accès refusé : rôle administrateur requis");
|
|
return;
|
|
}
|
|
}, [isLoading, isAuthenticated, user, router]);
|
|
|
|
const handleLogout = async () => {
|
|
await logout();
|
|
router.push("/login");
|
|
};
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50">
|
|
<Loading size="lg" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (!isAuthenticated || (user?.role !== "ADMIN" && user?.role !== "admin")) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="flex min-h-screen bg-gray-50">
|
|
<Sidebar />
|
|
<div className="flex-1 flex flex-col">
|
|
{/* Admin Header */}
|
|
<header className="bg-white shadow-sm border-b border-gray-200 px-6 py-4">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-4">
|
|
<Logo size="md" showText={false} />
|
|
<div>
|
|
<h1 className="text-2xl font-bold text-gray-900">Thé Tip Top - Administration</h1>
|
|
<p className="text-sm text-gray-500 mt-1">
|
|
Connecté en tant que <span className="font-medium">{user?.firstName} {user?.lastName}</span>
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={handleLogout}
|
|
className="flex items-center gap-2 px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
|
|
>
|
|
<LogOut className="w-4 h-4" />
|
|
Déconnexion
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
{/* Main Content */}
|
|
<main className="flex-1 overflow-auto">{children}</main>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|