Skip to content

Commit d722d43

Browse files
committed
fix: reject duplicate company names owned by other users on insert
When insert hits a name unique violation, only reuse the existing row if owner_id matches the authenticated user; otherwise return the same error as the edit path.
1 parent 6baf7c5 commit d722d43

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

apps/cursor/src/actions/upsert-company.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ export const upsertCompanyAction = authActionClient
121121
if (error.code === UNIQUE_VIOLATION) {
122122
const { data: existing } = await supabase
123123
.from("companies")
124-
.select("id, slug")
124+
.select("id, slug, owner_id")
125125
.eq("name_key", nameKey)
126126
.maybeSingle();
127127

128-
if (existing) {
128+
if (existing && existing.owner_id === userId) {
129129
if (shouldRedirect) {
130130
redirect(`/c/${existing.slug}`);
131131
}
132132

133-
return existing;
133+
return { id: existing.id, slug: existing.slug };
134134
}
135135

136136
throw new ActionError("A company with this name already exists.");

0 commit comments

Comments
 (0)