Skip to content

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

fix: prevent orphaned and duplicate orgs in personal org auto-creation

Scott Wilson44dd38bccb61
diff --git a/frontend/src/pages/organization/NoOrgPage/NoOrgPage.tsx b/frontend/src/pages/organization/NoOrgPage/NoOrgPage.tsx
index 32a3a3ea10..5ae4a42174 100644
--- a/frontend/src/pages/organization/NoOrgPage/NoOrgPage.tsx
+++ b/frontend/src/pages/organization/NoOrgPage/NoOrgPage.tsx
@@ -1,18 +1,23 @@
import { useEffect, useRef, useState } from "react";
import { Helmet } from "react-helmet";
import { useTranslation } from "react-i18next";
+import { useQueryClient } from "@tanstack/react-query";
import { useNavigate } from "@tanstack/react-router";
import { createNotification } from "@app/components/notifications";
import { CreateOrgModal } from "@app/components/organization/CreateOrgModal";
import { ContentLoader } from "@app/components/v2";
-import { useCreateOrg, useSelectOrganization } from "@app/hooks/api";
+import { organizationKeys, useCreateOrg, useSelectOrganization } from "@app/hooks/api";
const PERSONAL_ORG_NAME = "Personal Org";
+// A useRef gate resets on a full unmount/remount; this sessionStorage flag survives it, so an
+// interrupted-then-remounted page can't fire a second createOrg and leave a duplicate org behind.
+const CREATION_STARTED_KEY = "personalOrgCreationStarted";
export const NoOrgPage = () => {
const { t } = useTranslation();
const navigate = useNavigate();
+ const queryClient = useQueryClient();
const { mutateAsync: createOrg } = useCreateOrg({ invalidate: false });
const { mutateAsync: selectOrg } = useSelectOrganization();
@@ -25,10 +30,25 @@ export const NoOrgPage = () => {
if (hasRun.current) return;
hasRun.current = true;
+ if (sessionStorage.getItem(CREATION_STARTED_KEY)) return;
+ sessionStorage.setItem(CREATION_STARTED_KEY, "true");
+
const createPersonalOrg = async () => {
+ let organization;
try {
- const organization = await createOrg({ name: PERSONAL_ORG_NAME });
+ organization = await createOrg({ name: PERSONAL_ORG_NAME });
+ } catch {
+ // Nothing was created, so let the user create an org manually.
+ createNotification({
+ text: "Couldn't create your organization automatically. Please create one below.",
+ type: "error"
+ });
+ setFailed(true);
+ sessionStorage.removeItem(CREATION_STARTED_KEY);
+ return;
+ }
+ try {
await selectOrg({ organizationId: organization.id });
localStorage.setItem("orgData.id", organization.id);
@@ -38,16 +58,22 @@ export const NoOrgPage = () => {
params: { orgId: organization.id }
});
} catch {
+ // The org was created but selecting it failed. Showing CreateOrgModal here would create a
+ // second, orphaned org, so refresh the cached org list and hand off to the org picker,
+ // which auto-selects the now-existing single org and finishes login.
createNotification({
- text: "Couldn't create your organization automatically. Please create one below.",
+ text: "Created your organization, but couldn't open it automatically. Redirecting...",
type: "error"
});
- setFailed(true);
+ await queryClient.invalidateQueries({ queryKey: organizationKeys.getUserOrganizations });
+ navigate({ to: "/login/select-organization" });
+ } finally {
+ sessionStorage.removeItem(CREATION_STARTED_KEY);
}
};
createPersonalOrg();
- }, [createOrg, selectOrg, navigate]);
+ }, [createOrg, selectOrg, navigate, queryClient]);
return (
<>