Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(auth): address coderabbit/qodo review on workspace-creation gating

pesnike70d5417d44e
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index 40053006d..5af652f4d 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -374,8 +374,23 @@ export const auth = betterAuth({
// instance-wide role is meaningful here; per-workspace roles
// (owner/admin/member/viewer) don't apply until after a workspace
// is joined.
+ //
+ // `user` here comes from the session, which may be served out of
+ // the cookie cache (see `session.cookieCache` below). The
+ // first-user bootstrap promotes the user to admin in
+ // `databaseHooks.user.create.after`, but that happens after
+ // `signUpEmail` has already returned/cached the pre-promotion
+ // role, so a cached session can still say `role: "user"` for up
+ // to `cookieCache.maxAge`. Re-read the role from the database
+ // instead of trusting the (possibly stale) cached role.
allowUserToCreateOrganization: isWorkspaceCreationDisabled
- ? (user) => user.role === "admin"
+ ? async (user) => {
+ const [freshUser] = await db
+ .select({ role: schema.userTable.role })
+ .from(schema.userTable)
+ .where(eq(schema.userTable.id, user.id));
+ return freshUser?.role === "admin";
+ }
: true,
// Better Auth defaults this to `true`, which blocks any user whose email
// is not verified from accepting/rejecting an invitation. Kaneo does not
diff --git a/apps/web/src/components/command-palette/index.tsx b/apps/web/src/components/command-palette/index.tsx
index e16deeaa8..0f79ff8ca 100644
--- a/apps/web/src/components/command-palette/index.tsx
+++ b/apps/web/src/components/command-palette/index.tsx
@@ -51,8 +51,9 @@ function CommandPalette() {
const { data: workspace } = useActiveWorkspace();
const { data: session } = authClient.useSession();
const { data: config } = useGetConfig();
+ const isAdmin = session?.user?.role === "admin";
const canCreateWorkspace =
- !config?.disableWorkspaceCreation || session?.user?.role === "admin";
+ isAdmin || (config !== undefined && !config.disableWorkspaceCreation);
const [open, setOpen] = useState(false);
const [isSearchOpen, setIsSearchOpen] = useState(false);
const [isCreateTaskOpen, setIsCreateTaskOpen] = useState(false);
@@ -91,6 +92,7 @@ function CommandPalette() {
},
[shortcuts.workspace.prefix]: {
[shortcuts.workspace.create]: () => {
+ if (!canCreateWorkspace) return;
setIsCreateWorkspaceOpen(true);
},
},
diff --git a/apps/web/src/components/workspace-switcher.tsx b/apps/web/src/components/workspace-switcher.tsx
index c97fce2aa..5dc9a4877 100644
--- a/apps/web/src/components/workspace-switcher.tsx
+++ b/apps/web/src/components/workspace-switcher.tsx
@@ -41,8 +41,9 @@ export function WorkspaceSwitcher() {
const { data: workspaces } = useGetWorkspaces();
const { data: session } = authClient.useSession();
const { data: config } = useGetConfig();
+ const isAdmin = session?.user?.role === "admin";
const canCreateWorkspace =
- !config?.disableWorkspaceCreation || session?.user?.role === "admin";
+ isAdmin || (config !== undefined && !config.disableWorkspaceCreation);
const navigate = useNavigate();
const [isOpen, setIsOpen] = React.useState(false);
const [isCreateWorkspaceModalOpen, setIsCreateWorkspaceModalOpen] =