From 1126890afae9ef5c6b0d1cda0f28cf5f3d52ba27 Mon Sep 17 00:00:00 2001 From: karishmas6 Date: Wed, 16 Oct 2024 20:13:44 +0530 Subject: [PATCH] feat: access all gsheet files from gdrive --- server/src/routes/auth.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/server/src/routes/auth.ts b/server/src/routes/auth.ts index 9b3c98a0..0c8034b3 100644 --- a/server/src/routes/auth.ts +++ b/server/src/routes/auth.ts @@ -225,11 +225,23 @@ router.get('/google/callback', async (req, res) => { }, { where: { email } }); } + // List user's Google Sheets from their Google Drive + const drive = google.drive({ version: 'v3', auth: oauth2Client }); + const response = await drive.files.list({ + q: "mimeType='application/vnd.google-apps.spreadsheet'", // List only Google Sheets files + fields: 'files(id, name)', // Retrieve the ID and name of each file + }); + + const files = response.data.files || []; + if (files.length === 0) { + return res.status(404).json({ message: 'No spreadsheets found.' }); + } + // Generate JWT token for session const jwtToken = jwt.sign({ userId: user.id }, process.env.JWT_SECRET as string, { expiresIn: '12h' }); res.cookie('token', jwtToken, { httpOnly: true }); - res.json({ message: 'Google authentication successful', user, jwtToken }); + res.json({ message: 'Google authentication successful', user, jwtToken, files }); } catch (error) { res.status(500).json({ message: `Google OAuth error: ${error.message}` }); }