From 8dd9b8d74b527ce1cf0801cb9d415467d920f76a Mon Sep 17 00:00:00 2001 From: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Date: Sat, 25 Mar 2023 17:19:33 -0400 Subject: [PATCH] Fix Zod errors in Outlook auth function (#7931) * Add quick fixes * Add error message * Wooops --- .../office365calendar/lib/CalendarService.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/app-store/office365calendar/lib/CalendarService.ts b/packages/app-store/office365calendar/lib/CalendarService.ts index 14bcdb8c6e..7eadee15bc 100644 --- a/packages/app-store/office365calendar/lib/CalendarService.ts +++ b/packages/app-store/office365calendar/lib/CalendarService.ts @@ -219,7 +219,7 @@ export default class Office365CalendarService implements Calendar { }; const o365AuthCredentials = credential.key as O365AuthCredentials; - const refreshAccessToken = async (refreshToken: string) => { + const refreshAccessToken = async (o365AuthCredentials: O365AuthCredentials) => { const { client_id, client_secret } = await getOfficeAppKeys(); const response = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", { method: "POST", @@ -227,12 +227,22 @@ export default class Office365CalendarService implements Calendar { body: new URLSearchParams({ scope: "User.Read Calendars.Read Calendars.ReadWrite", client_id, - refresh_token: refreshToken, + refresh_token: o365AuthCredentials.refresh_token, grant_type: "refresh_token", client_secret, }), }); - const o365AuthCredentials = refreshTokenResponseSchema.parse(await handleErrorsJson(response)); + const responseJson = await handleErrorsJson(response); + const tokenResponse = refreshTokenResponseSchema.safeParse(responseJson); + o365AuthCredentials = { ...o365AuthCredentials, ...(tokenResponse.success && tokenResponse.data) }; + if (!tokenResponse.success) { + console.error( + "Outlook error grabbing new tokens ~ zodError:", + tokenResponse.error, + "MS response:", + responseJson + ); + } await prisma.credential.update({ where: { id: credential.id, @@ -246,9 +256,10 @@ export default class Office365CalendarService implements Calendar { return { getToken: () => + refreshTokenResponseSchema.safeParse(o365AuthCredentials).success && !isExpired(o365AuthCredentials.expires_in) ? Promise.resolve(o365AuthCredentials.access_token) - : refreshAccessToken(o365AuthCredentials.refresh_token), + : refreshAccessToken(o365AuthCredentials), }; };