From 598a86426b1b8ed6e7f6d1a19800859f4b158428 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Thu, 22 Apr 2021 11:09:18 +0000 Subject: [PATCH] Fixes adding google integration not appearing in the dashboard after adding it. This was due to the credential being added async and the redirect happened before the save has completed. --- pages/api/integrations/googlecalendar/callback.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pages/api/integrations/googlecalendar/callback.ts b/pages/api/integrations/googlecalendar/callback.ts index 8063d675..a4dac4ac 100644 --- a/pages/api/integrations/googlecalendar/callback.ts +++ b/pages/api/integrations/googlecalendar/callback.ts @@ -27,9 +27,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) const oAuth2Client = new google.auth.OAuth2(client_id, client_secret, redirect_uris[0]); // Convert to token - oAuth2Client.getToken(code, async (err, token) => { + return new Promise( (resolve, reject) => oAuth2Client.getToken(code, async (err, token) => { if (err) return console.error('Error retrieving access token', err); - + const credential = await prisma.credential.create({ data: { type: 'google_calendar', @@ -37,9 +37,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) userId: user.id } }); - }); - // Add the credential - - res.redirect('/integrations'); + res.redirect('/integrations'); + resolve(); + })); } \ No newline at end of file