From 1bf56fbe93e079f78186064bad874e56983fb6a4 Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Wed, 18 Oct 2023 21:35:24 -0400 Subject: [PATCH] fix: When GCal OAuth Canceled, Do Not Create A Credential (#11987) --- .../app-store/googlecalendar/api/callback.ts | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/packages/app-store/googlecalendar/api/callback.ts b/packages/app-store/googlecalendar/api/callback.ts index ccc2fe296e..4c80d974ec 100644 --- a/packages/app-store/googlecalendar/api/callback.ts +++ b/packages/app-store/googlecalendar/api/callback.ts @@ -16,10 +16,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const { code } = req.query; const state = decodeOAuthState(req); - if (code && typeof code !== "string") { + if (typeof code !== "string") { res.status(400).json({ message: "`code` must be a string" }); return; } + if (!req.session?.user?.id) { return res.status(401).json({ message: "You must be logged in to do this" }); } @@ -39,16 +40,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) if (code) { const token = await oAuth2Client.getToken(code); key = token.res?.data; - } - await prisma.credential.create({ - data: { - type: "google_calendar", - key, - userId: req.session.user.id, - appId: "google-calendar", - }, - }); + await prisma.credential.create({ + data: { + type: "google_calendar", + key, + userId: req.session.user.id, + appId: "google-calendar", + }, + }); + } if (state?.installGoogleVideo) { const existingGoogleMeetCredential = await prisma.credential.findFirst({