Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: With Credential Sync enabled, fix expired google calendar token handling - Refresh wasn't working #14788

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/app-store/googlecalendar/lib/CalendarService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a duplicate code line(of line 85)

}

private async getMyGoogleAuthSingleton() {
const googleCredentials = OAuth2UniversalSchema.parse(this.credential.key);
if (this.myGoogleAuth) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplified the code.

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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now setCredentials is also called only the first time. As getMyGoogleAuthSingleton can be called multiple times, we shouldn't set it again and again.

Rest of the code should ensure that setCredentials is called when needed

return this.myGoogleAuth;
}
Expand Down Expand Up @@ -151,14 +153,18 @@ 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,
},
data: {
key: token,
},
});

// Update cached credential as well
this.credential.key = key;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is important line which fixes the issue.

We update the token in DB. But in case of google-calendar there is a cached value here as well. We should keep it upto-date in case someone use this value.

},
});
this.oAuthManagerInstance = auth;
Expand Down