"use client"; import React, { useEffect, useState } from "react"; import Link from "next/link"; import { usePathname, useRouter } from "next/navigation"; import { getClientToken, removeClientToken } from "./_components/AdminComponents"; import { AdminLoginForm } from "./_components/AdminLoginForm"; import { adminVerifyToken } from "../../lib/adminApi"; export default function AdminLayout({ children }: { children: React.ReactNode }) { const pathname = usePathname(); const router = useRouter(); const [isAuthenticated, setIsAuthenticated] = useState(null); useEffect(() => { let isMounted = true; async function checkAuth() { const token = getClientToken(); if (!token) { if (isMounted) setIsAuthenticated(false); return; } const isValid = await adminVerifyToken(token); if (!isMounted) return; if (!isValid) { removeClientToken(); setIsAuthenticated(false); } else { setIsAuthenticated(true); } } checkAuth(); return () => { isMounted = false; }; }, [pathname]); if (pathname === "/console/login") { return (
{ setIsAuthenticated(true); router.push("/console"); }} />
); } if (isAuthenticated === null) { const token = typeof window !== "undefined" ? getClientToken() : null; if (!token) { return (
setIsAuthenticated(true)} />
); } return (
Checking authentication...
); } if (isAuthenticated === false) { return (
setIsAuthenticated(true)} />
); } const navItems = [ { href: "/console", label: "Dashboard Overview" }, { href: "/console/research-projects", label: "Research Projects" }, { href: "/console/research-areas", label: "Research Areas" }, { href: "/console/members", label: "Members & Alumni" }, { href: "/console/publications", label: "Publications & Patents" }, { href: "/console/lectures", label: "Lectures & Courses" }, { href: "/console/standardization", label: "Standardization" }, ]; const handleLogout = () => { removeClientToken(); setIsAuthenticated(false); }; return (
{/* Sidebar */} {/* Main Content Area */}
{children}
); }