Fix Zod errors in Outlook auth function (#7931)
* Add quick fixes * Add error message * Wooopspull/7931/merge
parent
0365157638
commit
8dd9b8d74b
|
@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue