Skip to content

Scheduling infrastructure for absolutely everyone.

Fix Zod errors in Outlook auth function (#7931)

Joe Au-Yeung8dd9b8d74b52
diff --git a/packages/app-store/office365calendar/lib/CalendarService.ts b/packages/app-store/office365calendar/lib/CalendarService.ts
index 14bcdb8c6e8..7eadee15bc8 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),
};
};