Fix Zod errors in Outlook auth function (#7931)

* Add quick fixes

* Add error message

* Wooops
pull/7931/merge
Joe Au-Yeung 2023-03-25 17:19:33 -04:00 committed by GitHub
parent 0365157638
commit 8dd9b8d74b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 4 deletions

View File

@ -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),
};
};