feat: cron done, admin unfinished
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { useEffect, useState, type JSX } from "react";
|
||||
import { Navigate, useLocation } from "react-router-dom";
|
||||
import { getCurrentUser } from "../helper/auth";
|
||||
|
||||
export const RequireAdmin = ({ children }: { children: JSX.Element }) => {
|
||||
const [isAdmin, setIsAdmin] = useState<boolean | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const location = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
const checkAdmin = async () => {
|
||||
const user = await getCurrentUser();
|
||||
setIsAdmin(user.isAdmin);
|
||||
setLoading(false);
|
||||
};
|
||||
checkAdmin();
|
||||
}, []);
|
||||
|
||||
if (loading) return <div>Checking permissions...</div>;
|
||||
|
||||
if (!isAdmin) {
|
||||
return <Navigate to="/unauthorized" state={{ from: location }} replace />;
|
||||
}
|
||||
|
||||
return children;
|
||||
};
|
||||
Reference in New Issue
Block a user