mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-03 03:01:52 +02:00
🐛 Fix broken account linking when using SSO (#1019)
This commit is contained in:
parent
8ea2cafbc4
commit
42a133e991
2 changed files with 8 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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: {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue