🐛 Fix account linking when using SSO (#1016)

This commit is contained in:
Luke Vella 2024-02-06 11:37:56 +07:00 committed by GitHub
parent 996ca88840
commit 213152e652
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View file

@ -222,8 +222,8 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
} else { } else {
// merge guest user into newly logged in user // merge guest user into newly logged in user
const session = await getServerSession(...args); const session = await getServerSession(...args);
if (session && session.user.email === null) { if (session && user.email && session.user.email === null) {
await mergeGuestsIntoUser(user.id, [session.user.id]); await mergeGuestsIntoUser(user.email, [session.user.id]);
} }
posthog?.capture({ posthog?.capture({

View file

@ -1,9 +1,23 @@
import { prisma } from "@rallly/database"; import { prisma } from "@rallly/database";
import * as Sentry from "@sentry/node";
export const mergeGuestsIntoUser = async ( export const mergeGuestsIntoUser = async (
userId: string, email: string,
guestIds: 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({ await prisma.poll.updateMany({
where: { where: {
userId: { userId: {