fix(integration): 🐛 Attempt to fix Google Sheets refresh
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { Credentials as CredentialsFromDb } from 'db'
|
import { Credentials as CredentialsFromDb } from 'db'
|
||||||
import { OAuth2Client } from 'google-auth-library'
|
import { OAuth2Client, Credentials } from 'google-auth-library'
|
||||||
import { GoogleSheetsCredentialsData } from 'models'
|
import { GoogleSheetsCredentialsData } from 'models'
|
||||||
import { decrypt, encrypt } from 'utils'
|
import { decrypt, encrypt } from 'utils'
|
||||||
import prisma from './prisma'
|
import prisma from './prisma'
|
||||||
@@ -22,16 +22,21 @@ export const getAuthenticatedGoogleClient = async (
|
|||||||
credentials.data,
|
credentials.data,
|
||||||
credentials.iv
|
credentials.iv
|
||||||
) as GoogleSheetsCredentialsData
|
) as GoogleSheetsCredentialsData
|
||||||
|
|
||||||
oauth2Client.setCredentials(data)
|
oauth2Client.setCredentials(data)
|
||||||
oauth2Client.on('tokens', updateTokens(credentialsId))
|
oauth2Client.on('tokens', updateTokens(credentialsId, data))
|
||||||
return oauth2Client
|
return oauth2Client
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateTokens =
|
const updateTokens =
|
||||||
(credentialsId: string) =>
|
(credentialsId: string, existingCredentials: GoogleSheetsCredentialsData) =>
|
||||||
async (credentials: GoogleSheetsCredentialsData) => {
|
async (credentials: Credentials) => {
|
||||||
const { encryptedData, iv } = encrypt(credentials)
|
const newCredentials = {
|
||||||
return prisma.credentials.update({
|
refresh_token: existingCredentials.refresh_token,
|
||||||
|
...credentials,
|
||||||
|
}
|
||||||
|
const { encryptedData, iv } = encrypt(newCredentials)
|
||||||
|
await prisma.credentials.update({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
data: { data: encryptedData, iv },
|
data: { data: encryptedData, iv },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,31 +4,37 @@ import { GoogleSheetsCredentialsData } from 'models'
|
|||||||
import { decrypt, encrypt } from 'utils'
|
import { decrypt, encrypt } from 'utils'
|
||||||
import prisma from './prisma'
|
import prisma from './prisma'
|
||||||
|
|
||||||
export const oauth2Client = new OAuth2Client(
|
|
||||||
process.env.GOOGLE_CLIENT_ID,
|
|
||||||
process.env.GOOGLE_CLIENT_SECRET,
|
|
||||||
`${process.env.NEXTAUTH_URL}/api/credentials/google-sheets/callback`
|
|
||||||
)
|
|
||||||
|
|
||||||
export const getAuthenticatedGoogleClient = async (
|
export const getAuthenticatedGoogleClient = async (
|
||||||
credentialsId: string
|
credentialsId: string
|
||||||
): Promise<OAuth2Client | undefined> => {
|
): Promise<OAuth2Client | undefined> => {
|
||||||
const credentials = (await prisma.credentials.findFirst({
|
const credentials = (await prisma.credentials.findFirst({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
})) as CredentialsFromDb
|
})) as CredentialsFromDb | undefined
|
||||||
const data = decrypt(credentials.data, credentials.iv) as
|
if (!credentials) return
|
||||||
| GoogleSheetsCredentialsData
|
const data = decrypt(
|
||||||
| undefined
|
credentials.data,
|
||||||
if (!data) return
|
credentials.iv
|
||||||
|
) as GoogleSheetsCredentialsData
|
||||||
|
|
||||||
|
const oauth2Client = new OAuth2Client(
|
||||||
|
process.env.GOOGLE_CLIENT_ID,
|
||||||
|
process.env.GOOGLE_CLIENT_SECRET,
|
||||||
|
`${process.env.NEXTAUTH_URL}/api/credentials/google-sheets/callback`
|
||||||
|
)
|
||||||
oauth2Client.setCredentials(data)
|
oauth2Client.setCredentials(data)
|
||||||
oauth2Client.on('tokens', updateTokens(credentialsId))
|
oauth2Client.on('tokens', updateTokens(credentialsId, data))
|
||||||
return oauth2Client
|
return oauth2Client
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateTokens =
|
const updateTokens =
|
||||||
(credentialsId: string) => async (credentials: Credentials) => {
|
(credentialsId: string, existingCredentials: GoogleSheetsCredentialsData) =>
|
||||||
const { encryptedData, iv } = encrypt(credentials)
|
async (credentials: Credentials) => {
|
||||||
return prisma.credentials.update({
|
const newCredentials = {
|
||||||
|
refresh_token: existingCredentials.refresh_token,
|
||||||
|
...credentials,
|
||||||
|
}
|
||||||
|
const { encryptedData, iv } = encrypt(newCredentials)
|
||||||
|
await prisma.credentials.update({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
data: { data: encryptedData, iv },
|
data: { data: encryptedData, iv },
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user