Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

Revert "Revert "fix(frontend): invalidate auth token after org select; default query staleTime""

Maidul Islam5a376ea55c00
diff --git a/frontend/CLAUDE.md b/frontend/CLAUDE.md
index 3b277b3959..c46e5f4752 100644
--- a/frontend/CLAUDE.md
+++ b/frontend/CLAUDE.md
@@ -48,6 +48,18 @@ Each API domain in `src/hooks/api/` (100+ domain folders) follows this structure
HTTP client in `src/config/request.ts`: Axios instance with automatic token injection and 401/403 interceptors.
+#### React Query Global Defaults (`src/hooks/api/reactQuery.tsx`)
+
+The `QueryClient` sets these global defaults for all queries:
+- **`staleTime: 60_000`** (60 seconds) — data fetched within the last 60s is considered fresh and won't be refetched on component mount/remount. This prevents redundant API calls during normal page navigation. Queries that need real-time data (e.g., identity auth configs, dynamic secret leases) override this with `staleTime: 0`.
+- **`refetchOnWindowFocus: false`** — queries do not refetch when the browser tab regains focus.
+- **`retry: 1`** — failed queries retry once.
+
+When adding new queries, consider whether the default 60s staleTime is appropriate:
+- For data that changes only on explicit user action (secrets, folders, org metadata): the 60s default is fine or could be longer.
+- For data that must always be fresh (auth configs, lease TTLs): override with `staleTime: 0, gcTime: 0`.
+- For rarely-changing data (server config, user profile): use `staleTime: Infinity` as the context providers do.
+
### State Management
- **Server state**: TanStack React Query (query key factories in each API domain)
diff --git a/frontend/src/hooks/api/accessApproval/queries.tsx b/frontend/src/hooks/api/accessApproval/queries.tsx
index 2be6c9535f..d9470634a4 100644
--- a/frontend/src/hooks/api/accessApproval/queries.tsx
+++ b/frontend/src/hooks/api/accessApproval/queries.tsx
@@ -104,7 +104,8 @@ export const useGetAccessRequestsCount = ({
queryKey: accessApprovalKeys.getAccessApprovalRequestCount(projectSlug, policyId),
queryFn: () => fetchAccessRequestsCount(projectSlug, policyId),
...options,
- enabled: Boolean(projectSlug) && (options?.enabled ?? true)
+ enabled: Boolean(projectSlug) && (options?.enabled ?? true),
+ refetchInterval: options?.refetchInterval ?? 30_000
});
export const useGetAccessApprovalPolicies = ({
@@ -131,5 +132,6 @@ export const useGetAccessApprovalRequests = ({
queryFn: () => fetchApprovalRequests({ projectSlug, envSlug, authorUserId }),
...options,
enabled: Boolean(projectSlug) && (options?.enabled ?? true),
- placeholderData: (previousData) => previousData
+ placeholderData: (previousData) => previousData,
+ refetchInterval: options?.refetchInterval ?? 30_000
});
diff --git a/frontend/src/hooks/api/auth/queries.tsx b/frontend/src/hooks/api/auth/queries.tsx
index 488a2667be..f920941544 100644
--- a/frontend/src/hooks/api/auth/queries.tsx
+++ b/frontend/src/hooks/api/auth/queries.tsx
@@ -101,11 +101,9 @@ export const useSelectOrganization = () => {
return data;
},
onSuccess: () => {
- // Invalidate the auth token cache so the authenticate middleware
- // re-calls fetchAuthToken (which reads the new in-memory token with orgId)
queryClient.invalidateQueries({ queryKey: authKeys.getAuthToken });
queryClient.invalidateQueries({ queryKey: organizationKeys.getUserOrganizations });
- queryClient.invalidateQueries({ queryKey: projectKeys.getAllUserProjects });
+ queryClient.invalidateQueries({ queryKey: projectKeys.getAllUserProjects() });
}
});
};
@@ -253,6 +251,8 @@ export const useGetAuthToken = () =>
useQuery({
queryKey: authKeys.getAuthToken,
queryFn: fetchAuthToken,
+ staleTime: 0,
+ gcTime: 0,
retry: 0
});
diff --git a/frontend/src/hooks/api/dynamicSecretLease/queries.ts b/frontend/src/hooks/api/dynamicSecretLease/queries.ts
index 095ece0f73..c7e64dbd36 100644
--- a/frontend/src/hooks/api/dynamicSecretLease/queries.ts
+++ b/frontend/src/hooks/api/dynamicSecretLease/queries.ts
@@ -24,6 +24,8 @@ export const useGetDynamicSecretLeases = ({
dynamicSecretName
}),
enabled: Boolean(projectSlug && environmentSlug && path && dynamicSecretName && enabled),
+ staleTime: 0,
+ gcTime: 0,
queryFn: async () => {
const { data } = await apiRequest.get<{ leases: TDynamicSecretLease[] }>(
`/api/v1/dynamic-secrets/${dynamicSecretName}/leases`,
diff --git a/frontend/src/hooks/api/identities/queries.tsx b/frontend/src/hooks/api/identities/queries.tsx
index 5614802ceb..39e88b338c 100644
--- a/frontend/src/hooks/api/identities/queries.tsx
+++ b/frontend/src/hooks/api/identities/queries.tsx
@@ -330,9 +330,11 @@ export const useGetIdentityLdapAuth = (
});
};
-export const useGetIdentityTokensTokenAuth = (identityId: string) => {
+export const useGetIdentityTokensTokenAuth = (
+ identityId: string,
+ options?: TReactQueryOptions["options"]
+) => {
return useQuery({
- enabled: Boolean(identityId),
queryKey: identitiesKeys.getIdentityTokensTokenAuth(identityId),
queryFn: async () => {
const {
@@ -341,7 +343,11 @@ export const useGetIdentityTokensTokenAuth = (identityId: string) => {
`/api/v1/auth/token-auth/identities/${identityId}/tokens`
);
return tokens;
- }
+ },
+ staleTime: 0,
+ gcTime: 0,
+ ...options,
+ enabled: Boolean(identityId) && (options?.enabled ?? true)
});
};
diff --git a/frontend/src/hooks/api/reactQuery.tsx b/frontend/src/hooks/api/reactQuery.tsx
index b80393cf51..b7a558dad2 100644
--- a/frontend/src/hooks/api/reactQuery.tsx
+++ b/frontend/src/hooks/api/reactQuery.tsx
@@ -220,7 +220,8 @@ export const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
- retry: 1
+ retry: 1,
+ staleTime: 60_000 // 60s — prevents refetch on every component mount/remount
}
}
});
diff --git a/frontend/src/hooks/api/secretApprovalRequest/mutation.tsx b/frontend/src/hooks/api/secretApprovalRequest/mutation.tsx
index 6a03b431ca..52f37848f1 100644
--- a/frontend/src/hooks/api/secretApprovalRequest/mutation.tsx
+++ b/frontend/src/hooks/api/secretApprovalRequest/mutation.tsx
@@ -20,8 +20,10 @@ export const useUpdateSecretApprovalReviewStatus = () => {
});
return data;
},
- onSuccess: (_, { id }) => {
+ onSuccess: (_, { id, projectId }) => {
queryClient.invalidateQueries({ queryKey: secretApprovalRequestKeys.detail({ id }) });
+ queryClient.invalidateQueries({ queryKey: secretApprovalRequestKeys.list({ projectId }) });
+ queryClient.invalidateQueries({ queryKey: secretApprovalRequestKeys.count({ projectId }) });
}
});
};
diff --git a/frontend/src/hooks/api/secretApprovalRequest/types.ts b/frontend/src/hooks/api/secretApprovalRequest/types.ts
index 38bd1c2796..e65d3de5e3 100644
--- a/frontend/src/hooks/api/secretApprovalRequest/types.ts
+++ b/frontend/src/hooks/api/secretApprovalRequest/types.ts
@@ -134,6 +134,7 @@ export type TUpdateSecretApprovalReviewStatusDTO = {
status: ApprovalStatus;
comment?: string;
id: string;
+ projectId: string;
};
export type TUpdateSecretApprovalRequestStatusDTO = {
diff --git a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/ProjectNav.tsx b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/ProjectNav.tsx
index 1950782bff..add508a3a7 100644
--- a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/ProjectNav.tsx
+++ b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/ProjectNav.tsx
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { useLocation, useNavigate } from "@tanstack/react-router";
import { AnimatePresence, motion } from "framer-motion";
-import { ChevronLeft, DoorOpen, FileKey, Shield } from "lucide-react";
+import { ChevronLeft } from "lucide-react";
import { SidebarGroup, SidebarGroupLabel, SidebarMenu } from "@app/components/v3";
import { useOrganization, useProject } from "@app/context";
@@ -31,6 +31,7 @@ import {
import { ProjectSubmenuView } from "./SubmenuViews";
import type { Submenu } from "./types";
import { PROJECT_TYPE_PATH } from "./types";
+import { useApprovalSubmenu } from "./useApprovalSubmenu";
const PROJECT_NAV_COMPONENT: Record<
ProjectType,
@@ -52,6 +53,8 @@ export const ProjectNav = () => {
const { currentOrg, isSubOrganization } = useOrganization();
const { pathname } = useLocation();
const navigate = useNavigate();
+ const { submenu: smApprovalsSubmenu, pendingRequestsCount: smPendingCount } =
+ useApprovalSubmenu();
const projectLabel = isSubOrganization ? "Sub-Organization" : "Organization";
const NavComponent = PROJECT_NAV_COMPONENT[currentProject.type];
@@ -90,17 +93,7 @@ export const ProjectNav = () => {
if (isOnCertApprovals) return CERT_APPROVALS_SUBMENU;
if (currentProject.type === ProjectType.PAM && pathname.includes("/approvals"))
return PAM_APPROVALS_SUBMENU;
- if (isOnApproval)
- return {
- title: "Approvals",
- pathSuffix: "approval",
- defaultTab: "approval-requests",
- items: [
- { label: "Change Requests", icon: FileKey, tab: "approval-requests" },
- { label: "Access Requests", icon: DoorOpen, tab: "resource-requests" },
- { label: "Policies", icon: Shield, tab: "policies" }
- ]
- };
+ if (isOnApproval) return smApprovalsSubmenu;
return null;
};
@@ -110,6 +103,16 @@ export const ProjectNav = () => {
setActiveSubmenu(getInitialProjectSubmenu());
}, [pathname]); // eslint-disable-line react-hooks/exhaustive-deps
+ // Keep the active submenu in sync when badge data changes
+ useEffect(() => {
+ setActiveSubmenu((current) => {
+ if (current && current.pathSuffix === smApprovalsSubmenu.pathSuffix) {
+ return smApprovalsSubmenu;
+ }
+ return current;
+ });
+ }, [smPendingCount]); // eslint-disable-line react-hooks/exhaustive-deps
+
const handleSubmenuOpen = (submenu: Submenu) => {
setActiveSubmenu(submenu);
const typePath = PROJECT_TYPE_PATH[currentProject.type];
diff --git a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/SecretManagerNav.tsx b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/SecretManagerNav.tsx
index fd031544fa..7a6f21392a 100644
--- a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/SecretManagerNav.tsx
+++ b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/SecretManagerNav.tsx
@@ -1,22 +1,8 @@
-import {
- ActivityIcon,
- BookCheck,
- DoorOpen,
- FileKey,
- FileText,
- Plug,
- RefreshCw,
- Settings,
- Shield
-} from "lucide-react";
+import { ActivityIcon, BookCheck, FileText, Plug, RefreshCw, Settings, Shield } from "lucide-react";
import { ProjectIcon } from "@app/components/v3";
import { useProject } from "@app/context";
-import {
- useGetAccessRequestsCount,
- useGetSecretApprovalRequestCount,
- useGetSecretRotations
-} from "@app/hooks/api";
+import { useGetSecretRotations } from "@app/hooks/api";
import { ProjectNavList } from "./ProjectNavLink";
import {
@@ -25,47 +11,21 @@ import {
SM_SETTINGS_SUBMENU
} from "./submenus";
import type { NavItem, Submenu } from "./types";
+import { useApprovalSubmenu } from "./useApprovalSubmenu";
export const SecretManagerNav = ({
onSubmenuOpen
}: {
onSubmenuOpen: (submenu: Submenu) => void;
}) => {
- const { currentProject, projectId } = useProject();
+ const { projectId } = useProject();
- const { data: secretApprovalReqCount } = useGetSecretApprovalRequestCount({ projectId });
- const { data: accessApprovalRequestCount } = useGetAccessRequestsCount({
- projectSlug: currentProject?.slug || ""
- });
+ const { submenu: approvalsSubmenu, pendingRequestsCount } = useApprovalSubmenu();
const { data: secretRotations } = useGetSecretRotations({
workspaceId: projectId,
options: { refetchOnMount: false }
});
- const pendingRequestsCount =
- (secretApprovalReqCount?.open || 0) + (accessApprovalRequestCount?.pendingCount || 0);
-
- const approvalsSubmenu: Submenu = {
- title: "Approvals",
- pathSuffix: "approval",
- defaultTab: "approval-requests",
- items: [
- {
- label: "Change Requests",
- icon: FileKey,
- tab: "approval-requests",
- badgeCount: secretApprovalReqCount?.open || undefined
- },
- {
- label: "Access Requests",
- icon: DoorOpen,
- tab: "resource-requests",
- badgeCount: accessApprovalRequestCount?.pendingCount || undefined
- },
- { label: "Policies", icon: Shield, tab: "policies" }
- ]
- };
-
const items: NavItem[] = [
{
label: "Overview",
diff --git a/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/useApprovalSubmenu.ts b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/useApprovalSubmenu.ts
new file mode 100644
index 0000000000..baf5187a42
--- /dev/null
+++ b/frontend/src/layouts/OrganizationLayout/components/OrgSidebar/useApprovalSubmenu.ts
@@ -0,0 +1,45 @@
+import { DoorOpen, FileKey, Shield } from "lucide-react";
+
+import { useProject } from "@app/context";
+import { useGetAccessRequestsCount, useGetSecretApprovalRequestCount } from "@app/hooks/api";
+
+import type { Submenu } from "./types";
+
+export const useApprovalSubmenu = (): {
+ submenu: Submenu;
+ pendingRequestsCount: number;
+} => {
+ const { currentProject, projectId } = useProject();
+
+ const { data: secretApprovalReqCount } = useGetSecretApprovalRequestCount({ projectId });
+ const { data: accessApprovalRequestCount } = useGetAccessRequestsCount({
+ projectSlug: currentProject?.slug || ""
+ });
+
+ const pendingRequestsCount =
+ (secretApprovalReqCount?.open || 0) + (accessApprovalRequestCount?.pendingCount || 0);
+
+ return {
+ submenu: {
+ title: "Approvals",
+ pathSuffix: "approval",
+ defaultTab: "approval-requests",
+ items: [
+ {
+ label: "Change Requests",
+ icon: FileKey,
+ tab: "approval-requests",
+ badgeCount: secretApprovalReqCount?.open || undefined
+ },
+ {
+ label: "Access Requests",
+ icon: DoorOpen,
+ tab: "resource-requests",
+ badgeCount: accessApprovalRequestCount?.pendingCount || undefined
+ },
+ { label: "Policies", icon: Shield, tab: "policies" }
+ ]
+ },
+ pendingRequestsCount
+ };
+};
diff --git a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChanges.tsx b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChanges.tsx
index a26977e242..64cc569125 100644
--- a/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChanges.tsx
+++ b/frontend/src/pages/secret-manager/SecretApprovalsPage/components/SecretApprovalRequest/components/SecretApprovalRequestChanges.tsx
@@ -188,7 +188,8 @@ export const SecretApprovalRequestChanges = ({ approvalRequestId, onGoBack }: Pr
await updateSecretApprovalRequestStatus({
id: approvalRequestId,
status,
- comment
+ comment,
+ projectId
});
createNotification({
type: "success",