feat: admin provider UI (types, hooks, guard, settings panel, conditional admin nav)
This commit is contained in:
27
k-tv-frontend/app/(main)/admin/layout.tsx
Normal file
27
k-tv-frontend/app/(main)/admin/layout.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
"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}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user