File size: 913 Bytes
7d51e81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"use client";

import { useEffect, useState } from "react";
import { notFound } from "next/navigation";

/**
 * AdminGuard — wraps any admin page child.
 * Reads role from localStorage (client-side) and calls notFound()
 * if the visitor is not an admin or superadmin, which renders
 * the app-level not-found.js (our custom 404 page).
 */
export default function AdminGuard({ children }) {
  const [status, setStatus] = useState("loading"); // "loading" | "ok" | "denied"

  useEffect(() => {
    const role = localStorage.getItem("role") || "";
    if (role === "admin" || role === "superadmin") {
      setStatus("ok");
    } else {
      setStatus("denied");
    }
  }, []);

  if (status === "loading") return null;
  if (status === "denied") return <Denied />;
  return children;
}

// Triggers Next.js not-found boundary → renders our custom 404 page
function Denied() {
  notFound();
  return null;
}