Skip to content

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

🐛 Fix broken account linking when using SSO (#1019)

Luke Vella42a133e99169
diff --git a/apps/web/src/utils/auth.ts b/apps/web/src/utils/auth.ts
index 00831159d..cff04b80a 100644
--- a/apps/web/src/utils/auth.ts
+++ b/apps/web/src/utils/auth.ts
@@ -222,8 +222,8 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
} else {
// merge guest user into newly logged in user
const session = await getServerSession(...args);
- if (session && user.email && session.user.email === null) {
- await mergeGuestsIntoUser(user.email, [session.user.id]);
+ if (session && session.user.email === null) {
+ await mergeGuestsIntoUser(user.id, [session.user.id]);
}
posthog?.capture({
@@ -238,7 +238,11 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
return true;
},
- async jwt({ token, user, trigger, session }) {
+ async jwt({ token, user, trigger, account, session }) {
+ if (trigger === "signUp" && account?.providerAccountId) {
+ // merge accounts assigned to provider account id to the current user id
+ await mergeGuestsIntoUser(user.id, [account.providerAccountId]);
+ }
if (trigger === "update" && session) {
if (token.email) {
// For registered users we want to save the preferences to the database
diff --git a/apps/web/src/utils/auth/merge-user.ts b/apps/web/src/utils/auth/merge-user.ts
index f7cc4da82..cf560fd43 100644
--- a/apps/web/src/utils/auth/merge-user.ts
+++ b/apps/web/src/utils/auth/merge-user.ts
@@ -1,23 +1,9 @@
import { prisma } from "@rallly/database";
-import * as Sentry from "@sentry/node";
export const mergeGuestsIntoUser = async (
- email: string,
+ userId: string,
guestIds: string[],
) => {
- const user = await prisma.user.findUnique({
- where: {
- email: email,
- },
- });
-
- const userId = user?.id;
-
- if (!userId) {
- Sentry.captureMessage("Could not find user to merge guests into.");
- return;
- }
-
await prisma.poll.updateMany({
where: {
userId: {