28 lines
779 B
TypeScript
28 lines
779 B
TypeScript
"use client";
|
|
|
|
import { useEffect, type ReactNode } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { useCurrentUser } from "@/hooks/use-auth";
|
|
import { useAuthContext } from "@/context/auth-context";
|
|
|
|
export default function AdminLayout({ children }: { children: ReactNode }) {
|
|
const { token, isLoaded } = useAuthContext();
|
|
const router = useRouter();
|
|
const { data: user, isLoading } = useCurrentUser();
|
|
|
|
useEffect(() => {
|
|
if (!isLoaded) return;
|
|
if (!token) {
|
|
router.replace("/login");
|
|
return;
|
|
}
|
|
if (!isLoading && user && !user.is_admin) {
|
|
router.replace("/dashboard");
|
|
}
|
|
}, [isLoaded, token, user, isLoading, router]);
|
|
|
|
if (!isLoaded || isLoading || !user?.is_admin) return null;
|
|
|
|
return <>{children}</>;
|
|
}
|