hotfix: fallback oauth find (#250)

This commit is contained in:
Jayvin Hernandez 2022-12-17 13:18:15 -08:00 committed by GitHub
parent bbeea5b0ec
commit 55eba480ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -58,7 +58,7 @@ export const withOAuth =
let existingOauth;
try {
existingOauth = await prisma.oAuth.findUnique({
existingOauth = await prisma.oAuth.findUniqueOrThrow({
where: {
provider_oauthId: {
provider: provider.toUpperCase() as OauthProviders,
@ -67,7 +67,8 @@ export const withOAuth =
},
});
} catch (e) {
if (e.code === 'P2002') {
logger.debug(`Failed to find existing oauth. Using fallback. ${e}`);
if (e.code === 'P2022' || e.code === 'P2025') {
const existing = await prisma.user.findFirst({
where: {
oauth: {
@ -82,6 +83,7 @@ export const withOAuth =
},
});
existingOauth = existing?.oauth?.find((o) => o.provider === provider.toUpperCase());
existingOauth.lastCase = true;
}
}
@ -153,7 +155,7 @@ export const withOAuth =
logger.info(`User ${user.username} (${user.id}) logged in via oauth(${provider})`);
return res.redirect('/dashboard');
} else if (existingOauth) {
} else if ((existingOauth && existingOauth.lastCase) || existingOauth) {
await prisma.oAuth.update({
where: {
id: existingOauth!.id,