mirror of
https://github.com/lukevella/rallly.git
synced 2025-04-29 02:06:34 +02:00
🐛 Fix data migration for social sign on (#1474)
This commit is contained in:
parent
f764ea9846
commit
340215da3f
2 changed files with 57 additions and 2 deletions
|
@ -24,7 +24,7 @@ import { getValueByPath } from "@/utils/get-value-by-path";
|
|||
import { decryptToken } from "@/utils/session";
|
||||
|
||||
import { CustomPrismaAdapter } from "./auth/custom-prisma-adapter";
|
||||
import { mergeGuestsIntoUser } from "./auth/merge-user";
|
||||
import { mergeGuestsIntoUser, temporarilyMigrateData } from "./auth/merge-user";
|
||||
|
||||
const providers: Provider[] = [
|
||||
// When a user registers, we don't want to go through the email verification process
|
||||
|
@ -242,7 +242,20 @@ const getAuthOptions = (...args: GetServerSessionParams) =>
|
|||
// merge guest user into newly logged in user
|
||||
const session = await getServerSession(...args);
|
||||
if (session && session.user.email === null) {
|
||||
await mergeGuestsIntoUser(user.id, [session.user.id]);
|
||||
// check if user exists
|
||||
const count = await prisma.user.count({
|
||||
where: {
|
||||
email: user.email as string,
|
||||
},
|
||||
});
|
||||
|
||||
if (count !== 0) {
|
||||
await mergeGuestsIntoUser(user.id, [session.user.id]);
|
||||
} else {
|
||||
// when logging in with a social account, the user doesn't exist yet
|
||||
// so we temporarily migrate the data to a different guest user.
|
||||
await temporarilyMigrateData(user.id, [session.user.id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,3 +44,45 @@ export const mergeGuestsIntoUser = async (
|
|||
]);
|
||||
});
|
||||
};
|
||||
|
||||
export const temporarilyMigrateData = async (
|
||||
providerId: string,
|
||||
guestIds: string[],
|
||||
) => {
|
||||
return await prisma.$transaction(async (tx) => {
|
||||
await Promise.all([
|
||||
tx.poll.updateMany({
|
||||
where: {
|
||||
guestId: {
|
||||
in: guestIds,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
guestId: providerId,
|
||||
},
|
||||
}),
|
||||
|
||||
tx.participant.updateMany({
|
||||
where: {
|
||||
guestId: {
|
||||
in: guestIds,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
guestId: providerId,
|
||||
},
|
||||
}),
|
||||
|
||||
tx.comment.updateMany({
|
||||
where: {
|
||||
guestId: {
|
||||
in: guestIds,
|
||||
},
|
||||
},
|
||||
data: {
|
||||
guestId: providerId,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue