hotfix: make oauthid optional (#249)

This commit is contained in:
Jayvin Hernandez 2022-12-17 09:37:29 -08:00 committed by GitHub
parent ad454a94ef
commit bbeea5b0ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 12 deletions

View file

@ -6,7 +6,7 @@
*/
-- AlterTable
ALTER TABLE "OAuth" ADD COLUMN "oauthId" TEXT NOT NULL;
ALTER TABLE "OAuth" ADD COLUMN "oauthId" TEXT;
-- CreateIndex
CREATE UNIQUE INDEX "OAuth_provider_oauthId_key" ON "OAuth"("provider", "oauthId");

View file

@ -56,14 +56,34 @@ export const withOAuth =
const { state } = req.query as { state?: string };
const existingOauth = await prisma.oAuth.findUnique({
where: {
provider_oauthId: {
provider: provider.toUpperCase() as OauthProviders,
oauthId: oauth_resp.user_id,
let existingOauth;
try {
existingOauth = await prisma.oAuth.findUnique({
where: {
provider_oauthId: {
provider: provider.toUpperCase() as OauthProviders,
oauthId: oauth_resp.user_id as string,
},
},
},
});
});
} catch (e) {
if (e.code === 'P2002') {
const existing = await prisma.user.findFirst({
where: {
oauth: {
some: {
provider: provider.toUpperCase() as OauthProviders,
username: oauth_resp.username,
},
},
},
include: {
oauth: true,
},
});
existingOauth = existing?.oauth?.find((o) => o.provider === provider.toUpperCase());
}
}
const existingUser = await prisma.user.findFirst({
where: {
@ -98,7 +118,7 @@ export const withOAuth =
token: oauth_resp.access_token,
refresh: oauth_resp.refresh_token || null,
username: oauth_resp.username,
oauthId: oauth_resp.user_id,
oauthId: oauth_resp.user_id as string,
},
},
avatar: oauth_resp.avatar,
@ -125,7 +145,7 @@ export const withOAuth =
token: oauth_resp.access_token,
refresh: oauth_resp.refresh_token || null,
username: oauth_resp.username,
oauthId: oauth_resp.user_id,
oauthId: oauth_resp.user_id as string,
},
});
@ -142,7 +162,7 @@ export const withOAuth =
token: oauth_resp.access_token,
refresh: oauth_resp.refresh_token || null,
username: oauth_resp.username,
oauthId: oauth_resp.user_id,
oauthId: oauth_resp.user_id as string,
},
});
@ -168,7 +188,7 @@ export const withOAuth =
token: oauth_resp.access_token,
refresh: oauth_resp.refresh_token || null,
username: oauth_resp.username,
oauthId: oauth_resp.user_id,
oauthId: oauth_resp.user_id as string,
},
},
avatar: oauth_resp.avatar,