: 🔒️ Investigate on why spreadsheets sometimes fail
This commit is contained in:
@ -13,9 +13,11 @@ export const oauth2Client = new OAuth2Client(
|
|||||||
export const getAuthenticatedGoogleClient = async (
|
export const getAuthenticatedGoogleClient = async (
|
||||||
userId: string,
|
userId: string,
|
||||||
credentialsId: string
|
credentialsId: string
|
||||||
): Promise<OAuth2Client | undefined> => {
|
): Promise<
|
||||||
const credentials = (await prisma.credentials.findUnique({
|
{ client: OAuth2Client; credentials: CredentialsFromDb } | undefined
|
||||||
where: { id: credentialsId },
|
> => {
|
||||||
|
const credentials = (await prisma.credentials.findFirst({
|
||||||
|
where: { id: credentialsId, ownerId: userId },
|
||||||
})) as CredentialsFromDb | undefined
|
})) as CredentialsFromDb | undefined
|
||||||
if (!credentials || credentials.ownerId !== userId) return
|
if (!credentials || credentials.ownerId !== userId) return
|
||||||
const data = decrypt(
|
const data = decrypt(
|
||||||
@ -25,7 +27,7 @@ export const getAuthenticatedGoogleClient = async (
|
|||||||
|
|
||||||
oauth2Client.setCredentials(data)
|
oauth2Client.setCredentials(data)
|
||||||
oauth2Client.on('tokens', updateTokens(credentialsId, data))
|
oauth2Client.on('tokens', updateTokens(credentialsId, data))
|
||||||
return oauth2Client
|
return { client: oauth2Client, credentials }
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateTokens =
|
const updateTokens =
|
||||||
|
@ -6,7 +6,7 @@ const handlers = () => [
|
|||||||
const authenticatedUser = JSON.parse(
|
const authenticatedUser = JSON.parse(
|
||||||
typeof localStorage !== 'undefined'
|
typeof localStorage !== 'undefined'
|
||||||
? (localStorage.getItem('authenticatedUser') as string)
|
? (localStorage.getItem('authenticatedUser') as string)
|
||||||
: '{"id":"proUser","name":"John Smith","email":"john@smith.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","plan":"PRO","stripeId":null}'
|
: '{"id":"proUser","name":"Pro user","email":"pro-user@email.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","plan":"PRO","stripeId":null}'
|
||||||
)
|
)
|
||||||
return res(
|
return res(
|
||||||
ctx.json({
|
ctx.json({
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { drive } from '@googleapis/drive'
|
import { drive } from '@googleapis/drive'
|
||||||
import { getAuthenticatedGoogleClient } from 'libs/google-sheets'
|
import { getAuthenticatedGoogleClient } from 'libs/google-sheets'
|
||||||
import { badRequest, methodNotAllowed, notAuthenticated } from 'utils'
|
import {
|
||||||
import { setUser, withSentry } from '@sentry/nextjs'
|
badRequest,
|
||||||
|
forbidden,
|
||||||
|
methodNotAllowed,
|
||||||
|
notAuthenticated,
|
||||||
|
} from 'utils'
|
||||||
|
import { captureException, setUser, withSentry } from '@sentry/nextjs'
|
||||||
import { getAuthenticatedUser } from 'services/api/utils'
|
import { getAuthenticatedUser } from 'services/api/utils'
|
||||||
|
|
||||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||||
@ -16,9 +21,18 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
||||||
if (!auth)
|
if (!auth)
|
||||||
return res.status(404).send("Couldn't find credentials in database")
|
return res.status(404).send("Couldn't find credentials in database")
|
||||||
|
console.log(auth.credentials.name, user.email)
|
||||||
|
if (auth.credentials.name !== user.email) {
|
||||||
|
captureException(
|
||||||
|
new Error(
|
||||||
|
`Credentials name does not match user email ${auth?.credentials.name} !== ${user.email}`
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return forbidden(res)
|
||||||
|
}
|
||||||
const response = await drive({
|
const response = await drive({
|
||||||
version: 'v3',
|
version: 'v3',
|
||||||
auth: auth,
|
auth: auth.client,
|
||||||
}).files.list({
|
}).files.list({
|
||||||
q: "mimeType='application/vnd.google-apps.spreadsheet'",
|
q: "mimeType='application/vnd.google-apps.spreadsheet'",
|
||||||
fields: 'nextPageToken, files(id, name)',
|
fields: 'nextPageToken, files(id, name)',
|
||||||
|
@ -21,12 +21,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
|
|
||||||
const spreadsheetId = req.query.id.toString()
|
const spreadsheetId = req.query.id.toString()
|
||||||
const doc = new GoogleSpreadsheet(spreadsheetId)
|
const doc = new GoogleSpreadsheet(spreadsheetId)
|
||||||
const client = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
const auth = await getAuthenticatedGoogleClient(user.id, credentialsId)
|
||||||
if (!client)
|
if (!auth)
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.send({ message: "Couldn't find credentials in database" })
|
.send({ message: "Couldn't find credentials in database" })
|
||||||
doc.useOAuth2Client(client)
|
doc.useOAuth2Client(auth.client)
|
||||||
await doc.loadInfo()
|
await doc.loadInfo()
|
||||||
return res.send({
|
return res.send({
|
||||||
sheets: (
|
sheets: (
|
||||||
|
@ -109,7 +109,7 @@ const createCredentials = () => {
|
|||||||
return prisma.credentials.createMany({
|
return prisma.credentials.createMany({
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
name: 'test2@gmail.com',
|
name: 'pro-user@email.com',
|
||||||
ownerId: 'proUser',
|
ownerId: 'proUser',
|
||||||
type: CredentialsType.GOOGLE_SHEETS,
|
type: CredentialsType.GOOGLE_SHEETS,
|
||||||
data: encryptedData,
|
data: encryptedData,
|
||||||
|
@ -152,7 +152,7 @@ test.describe.parallel('Google sheets integration', () => {
|
|||||||
const fillInSpreadsheetInfo = async (page: Page) => {
|
const fillInSpreadsheetInfo = async (page: Page) => {
|
||||||
await page.click('text=Configure...')
|
await page.click('text=Configure...')
|
||||||
await page.click('text=Select an account')
|
await page.click('text=Select an account')
|
||||||
await page.click('text=test2@gmail.com')
|
await page.click('text=pro-user@email.com')
|
||||||
|
|
||||||
await page.fill('input[placeholder="Search for spreadsheet"]', 'CR')
|
await page.fill('input[placeholder="Search for spreadsheet"]', 'CR')
|
||||||
await page.click('text=CRM')
|
await page.click('text=CRM')
|
||||||
|
Reference in New Issue
Block a user