Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

UN-2955 [FIX] Re-verify admin status on navigation to admin routes (#1773)

Kirtiman Mishra442992aa6202
diff --git a/frontend/src/components/helpers/auth/RequireAdmin.js b/frontend/src/components/helpers/auth/RequireAdmin.js
index c57f85241..856afaa00 100644
--- a/frontend/src/components/helpers/auth/RequireAdmin.js
+++ b/frontend/src/components/helpers/auth/RequireAdmin.js
@@ -1,22 +1,62 @@
+import { useEffect, useState } from "react";
import { Outlet } from "react-router-dom";
+import { useAxiosPrivate } from "../../../hooks/useAxiosPrivate";
import { useSessionStore } from "../../../store/session-store";
import { NotFound } from "../../error/NotFound/NotFound.jsx";
import { Unauthorized } from "../../error/UnAuthorized/Unauthorized.jsx";
const RequireAdmin = () => {
- const { sessionDetails } = useSessionStore();
- const isAdmin = sessionDetails?.isAdmin;
+ const axiosPrivate = useAxiosPrivate();
+ const { sessionDetails, updateSessionDetails } = useSessionStore();
+ const [isVerifying, setIsVerifying] = useState(true);
+ const orgId = sessionDetails?.orgId;
const orgName = sessionDetails?.orgName;
const isOpenSource = orgName === "mock_org";
- if (!isAdmin) {
+ useEffect(() => {
+ const verifyAdminStatus = async () => {
+ try {
+ const res = await axiosPrivate.get(
+ `/api/v1/unstract/${orgId}/users/profile/`,
+ { headers: { "X-CSRFToken": sessionDetails?.csrfToken } },
+ );
+ const currentIsAdmin = res?.data?.user?.is_admin;
+ const currentRole = res?.data?.user?.role;
+ const updates = {};
+ if (currentIsAdmin !== sessionDetails?.isAdmin) {
+ updates.isAdmin = currentIsAdmin;
+ }
+ if (currentRole && currentRole !== sessionDetails?.role) {
+ updates.role = currentRole;
+ }
+ if (Object.keys(updates).length > 0) {
+ updateSessionDetails(updates);
+ }
+ } catch {
+ // On error, keep current session state
+ } finally {
+ setIsVerifying(false);
+ }
+ };
+
+ if (orgId) {
+ verifyAdminStatus();
+ } else {
+ setIsVerifying(false);
+ }
+ }, [orgId]);
+
+ if (isVerifying) {
+ return null;
+ }
+
+ if (!sessionDetails?.isAdmin) {
return <Unauthorized />;
}
if (isOpenSource) {
return <NotFound />;
}
-
return <Outlet />;
};