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

This commit is contained in:
Luke Vella 2024-02-07 16:59:53 +07:00 committed by GitHub
parent 8ea2cafbc4
commit 42a133e991
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 18 deletions

View file

@ -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

View file

@ -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: {