From 900e90cd2c9b14489d09ee3c015ed719aa4e5b3a Mon Sep 17 00:00:00 2001 From: Hariom Balhara Date: Tue, 30 Apr 2024 02:07:18 +0530 Subject: [PATCH] fix: With Credential Sync enabled, fix expired google calendar token handling - Refresh wasn't working (#14788) Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> --- .../googlecalendar/lib/CalendarService.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/app-store/googlecalendar/lib/CalendarService.ts b/packages/app-store/googlecalendar/lib/CalendarService.ts index d3a7e638f5b22..fb21bb68a67d7 100644 --- a/packages/app-store/googlecalendar/lib/CalendarService.ts +++ b/packages/app-store/googlecalendar/lib/CalendarService.ts @@ -85,13 +85,15 @@ export default class GoogleCalendarService implements Calendar { this.credential = credential; this.auth = this.initGoogleAuth(credential); this.log = log.getSubLogger({ prefix: [`[[lib] ${this.integrationName}`] }); - this.credential = credential; } private async getMyGoogleAuthSingleton() { - const googleCredentials = OAuth2UniversalSchema.parse(this.credential.key); + if (this.myGoogleAuth) { + return this.myGoogleAuth; + } const { client_id, client_secret, redirect_uris } = await getGoogleAppKeys(); - this.myGoogleAuth = this.myGoogleAuth || new MyGoogleAuth(client_id, client_secret, redirect_uris[0]); + const googleCredentials = OAuth2UniversalSchema.parse(this.credential.key); + this.myGoogleAuth = new MyGoogleAuth(client_id, client_secret, redirect_uris[0]); this.myGoogleAuth.setCredentials(googleCredentials); return this.myGoogleAuth; } @@ -151,7 +153,8 @@ export default class GoogleCalendarService implements Calendar { }, updateTokenObject: async (token) => { this.myGoogleAuth.setCredentials(token); - await prisma.credential.update({ + + const { key } = await prisma.credential.update({ where: { id: credential.id, }, @@ -159,6 +162,9 @@ export default class GoogleCalendarService implements Calendar { key: token, }, }); + + // Update cached credential as well + this.credential.key = key; }, }); this.oAuthManagerInstance = auth;