Skip to content

Open source alternative to Semrush and Ahrefs

Stop re-minting free workspaces after a rename (#1064)

Ben Senescu3bb0778eb1bd
diff --git a/src/server/auth/default-hosted-organization.test.ts b/src/server/auth/default-hosted-organization.test.ts
index e51c3725..e5520aa5 100644
--- a/src/server/auth/default-hosted-organization.test.ts
+++ b/src/server/auth/default-hosted-organization.test.ts
@@ -50,6 +50,29 @@ describe("resolveSignInHostedOrganization", () => {
).resolves.toMatchObject({ organizationId: "org-new" });
});
+ // Billing invariant: each default org is a new billing customer with a
+ // free-plan grant, and the unique slug is what stops a second one. A user
+ // who left their org and renamed themselves must still collide.
+ it("keys the default organization slug on the user, not their name", async () => {
+ mocks.hasPendingInvitationForEmail.mockResolvedValue(false);
+ const createOrganization = vi.fn(async (_input: { slug: string }) => ({
+ id: "org-new",
+ }));
+
+ await resolveSignInHostedOrganization("user-1", createOrganization);
+ mocks.getHostedUser.mockResolvedValue({
+ id: "user-1",
+ email: "[email protected]",
+ name: "Renamed",
+ });
+ await resolveSignInHostedOrganization("user-1", createOrganization);
+
+ const [first, second] = createOrganization.mock.calls.map(
+ ([input]) => input.slug,
+ );
+ expect(second).toBe(first);
+ });
+
// An existing membership always wins — the pending-invite check only
// applies to membership-less users, so inviting an existing user never
// hides their current organization.
diff --git a/src/server/auth/default-hosted-organization.ts b/src/server/auth/default-hosted-organization.ts
index 39bd79aa..fa841425 100644
--- a/src/server/auth/default-hosted-organization.ts
+++ b/src/server/auth/default-hosted-organization.ts
@@ -1,6 +1,6 @@
import { AuthRepository } from "@/server/auth/repositories/AuthRepository";
import { markDubReferredOrganization } from "@/server/referrals/dub";
-import { slugify, toHex } from "./org-slug";
+import { toHex } from "./org-slug";
type HostedUser = {
id: string;
@@ -23,11 +23,12 @@ function getDefaultHostedOrganizationName(user: HostedUser) {
return `${name}'s organization`;
}
+// Keyed on the user id alone, never the editable display name: the slug is
+// unique, so a second default organization for the same user fails. A user
+// who hands their org to someone else and leaves would otherwise get a fresh
+// org, and a fresh free-plan credit grant, on every sign-in after a rename.
function getDefaultHostedOrganizationSlug(user: HostedUser) {
- const slugSource =
- user.name?.trim() || user.email.split("@")[0] || "organization";
- const suffix = toHex(user.id).slice(0, 12);
- return `${slugify(slugSource)}-${suffix}`;
+ return `org-${toHex(user.id)}`;
}
async function getHostedUser(userId: string) {