Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

πŸ› Keep a guest's content when linking it to an account fails

Luke Vellad846001cfca2
diff --git a/apps/web/src/features/auth/mutations.ts b/apps/web/src/features/auth/mutations.ts
index 3eb11e8c5..8015a6e15 100644
--- a/apps/web/src/features/auth/mutations.ts
+++ b/apps/web/src/features/auth/mutations.ts
@@ -29,6 +29,9 @@ export const linkAnonymousUser = async (
authenticatedUserId: string,
anonymousUserId: string,
) => {
+ let spaceId: string | undefined;
+ let adoptedOpenCount = 0;
+
try {
// Claim event registrations made with the account's own email. A
// registration's email is its identity (the host's record), so it must
@@ -53,7 +56,7 @@ export const linkAnonymousUser = async (
});
}
- const spaceId = await getActiveSpaceForUser({
+ spaceId = await getActiveSpaceForUser({
userId: authenticatedUserId,
});
@@ -62,68 +65,75 @@ export const linkAnonymousUser = async (
// been through /setup yet (setup creates the space and adopts these
// orphaned polls), or when the user's only memberships are
// ineffective (non-owner rows in hobby spaces). Transfer ownership
- // anyway β€” the anonymous user is deleted right after linking and
- // polls cascade with it β€” and leave spaceId null.
+ // anyway β€” the anonymous user is deleted once this returns merged
+ // and polls cascade with it β€” and leave spaceId null.
logger.info(
{ userId: authenticatedUserId },
"User has no effective space; migrating guest content without a space",
);
}
- const adoptedOpenCount = spaceId
- ? await countAdoptableOpenPolls({ userId: anonymousUserId })
- : 0;
-
- await prisma.$transaction(async (tx) => {
- // Transfer space-scoped data from anonymous user to authenticated user
- await Promise.all([
- // Transfer polls
- tx.poll.updateMany({
- where: {
- userId: anonymousUserId,
- },
- data: {
- userId: authenticatedUserId,
- ...(spaceId ? { spaceId } : {}),
- },
- }),
-
- // Transfer participants
- tx.participant.updateMany({
- where: {
- userId: anonymousUserId,
- },
- data: {
- userId: authenticatedUserId,
- },
- }),
-
- // Transfer comments
- tx.comment.updateMany({
- where: {
- userId: anonymousUserId,
- },
- data: {
- userId: authenticatedUserId,
- },
- }),
-
- // Transfer poll activity actor refs. They are soft references (no
- // FK), so nothing else migrates them; without this the guest's
- // events would point at the anonymous user forever, which is
- // deleted after linking. Account deletion leaves refs inert on
- // purpose β€” linking is the opposite case: the person still exists.
- tx.pollActivity.updateMany({
- where: {
- userId: anonymousUserId,
- },
- data: {
- userId: authenticatedUserId,
- },
- }),
- ]);
- });
+ if (spaceId) {
+ adoptedOpenCount = await countAdoptableOpenPolls({
+ userId: anonymousUserId,
+ });
+ }
+
+ // A batch, not an interactive transaction: the interactive form's 5s
+ // timeout rolled back merges whose commit landed late, and the guest was
+ // then deleted with everything still on it.
+ await prisma.$transaction([
+ // Transfer polls
+ prisma.poll.updateMany({
+ where: {
+ userId: anonymousUserId,
+ },
+ data: {
+ userId: authenticatedUserId,
+ ...(spaceId ? { spaceId } : {}),
+ },
+ }),
+
+ // Transfer participants
+ prisma.participant.updateMany({
+ where: {
+ userId: anonymousUserId,
+ },
+ data: {
+ userId: authenticatedUserId,
+ },
+ }),
+ // Transfer comments
+ prisma.comment.updateMany({
+ where: {
+ userId: anonymousUserId,
+ },
+ data: {
+ userId: authenticatedUserId,
+ },
+ }),
+
+ // Transfer poll activity actor refs. They are soft references (no
+ // FK), so nothing else migrates them; without this the guest's
+ // events would point at the anonymous user forever, which is
+ // deleted after linking. Account deletion leaves refs inert on
+ // purpose β€” linking is the opposite case: the person still exists.
+ prisma.pollActivity.updateMany({
+ where: {
+ userId: anonymousUserId,
+ },
+ data: {
+ userId: authenticatedUserId,
+ },
+ }),
+ ]);
+ } catch (error) {
+ Sentry.captureException(error);
+ return { merged: false };
+ }
+
+ try {
// Without a space, setup adopts the polls later and reports then.
if (spaceId) {
await reportAdoptionOverLimit({
@@ -143,4 +153,6 @@ export const linkAnonymousUser = async (
} catch (error) {
Sentry.captureException(error);
}
+
+ return { merged: true };
};
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 88fda82c1..56ac266e0 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -198,8 +198,29 @@ export const authLib = betterAuth({
const { t } = await getTranslation();
return t("guest");
},
- onLinkAccount: async ({ anonymousUser, newUser }) => {
- await linkAnonymousUser(newUser.user.id, anonymousUser.user.id);
+ // The plugin deletes the guest after onLinkAccount whether or not the
+ // merge worked, and the delete cascades to the guest's polls. Deletion
+ // happens below instead, only once the merge has committed; a guest
+ // whose merge failed keeps its content for support to reassign.
+ disableDeleteAnonymousUser: true,
+ onLinkAccount: async ({ anonymousUser, newUser, ctx }) => {
+ if (newUser.user.id === anonymousUser.user.id) return;
+
+ const { merged } = await linkAnonymousUser(
+ newUser.user.id,
+ anonymousUser.user.id,
+ );
+
+ if (!merged || newUser.user.isAnonymous) return;
+
+ try {
+ await ctx.context.internalAdapter.deleteUser(anonymousUser.user.id);
+ } catch (error) {
+ logger.error(
+ { anonymousUserId: anonymousUser.user.id, error },
+ "Failed to delete anonymous user after linking",
+ );
+ }
},
}),
lastLoginMethod({
diff --git a/apps/web/tests/guest-to-user-migration.spec.ts b/apps/web/tests/guest-to-user-migration.spec.ts
index 0e40eea16..9dfac6b4a 100644
--- a/apps/web/tests/guest-to-user-migration.spec.ts
+++ b/apps/web/tests/guest-to-user-migration.spec.ts
@@ -62,6 +62,11 @@ test.describe.serial(() => {
await expect(
page.getByRole("heading", { name: "Board Meeting" }),
).toBeVisible();
+ const guestPollId = page.url().match(/\/poll\/([^/?]+)/)?.[1];
+ const guestPoll = await prisma.poll.findUnique({
+ where: { id: guestPollId },
+ });
+ expect(guestPoll?.userId).toBeTruthy();
// Step 2: Navigate to registration
await page.getByRole("link", { name: "Login" }).click();
@@ -85,5 +90,11 @@ test.describe.serial(() => {
prisma.user.findUnique({ where: { email: testUser.email } }),
]);
expect(poll?.userId).toBe(user?.id);
+
+ // The guest is removed only once its content has moved
+ const guest = await prisma.user.findUnique({
+ where: { id: guestPoll?.userId ?? "" },
+ });
+ expect(guest).toBeNull();
});
});
diff --git a/packages/database/prisma/migrations/20261019120000_poll_activities_user_id_index/migration.sql b/packages/database/prisma/migrations/20261019120000_poll_activities_user_id_index/migration.sql
new file mode 100644
index 000000000..5120c06ed
--- /dev/null
+++ b/packages/database/prisma/migrations/20261019120000_poll_activities_user_id_index/migration.sql
@@ -0,0 +1,2 @@
+-- CreateIndex
+CREATE INDEX "poll_activities_user_id_idx" ON "poll_activities"("user_id");
diff --git a/packages/database/prisma/models/poll.prisma b/packages/database/prisma/models/poll.prisma
index ec58d0097..931ce8f9b 100644
--- a/packages/database/prisma/models/poll.prisma
+++ b/packages/database/prisma/models/poll.prisma
@@ -148,6 +148,7 @@ model PollActivity {
@@index([pollId, participantId])
@@index([pollId, optionId])
@@index([pollId, inviteId])
+ @@index([userId])
@@map("poll_activities")
}