fix: update user/s google sheet email

This commit is contained in:
karishmas6
2024-10-16 21:47:11 +05:30
parent 4a56cfbde9
commit 13266d64b5

View File

@@ -168,7 +168,7 @@ router.delete('/delete-api-key', requireSignIn, async (req, res) => {
const oauth2Client = new google.auth.OAuth2( const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_CLIENT_ID, process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET, process.env.GOOGLE_CLIENT_SECRET,
// process.env.GOOGLE_REDIRECT_URI process.env.GOOGLE_REDIRECT_URI
); );
// Step 1: Redirect to Google for authentication // Step 1: Redirect to Google for authentication
@@ -206,25 +206,20 @@ router.get('/google/callback', requireSignIn, async (req, res) => {
return res.status(400).json({ message: 'Email not found' }); return res.status(400).json({ message: 'Email not found' });
} }
// Check if user already exists // Get the currently authenticated user (from `requireSignIn`)
let user = await User.findOne({ where: { email } }); let user = await User.findOne({ where: { id: req.user.id } });
if (!user) { if (!user) {
const hashedPassword = await hashPassword(email + process.env.JWT_SECRET); return res.status(400).json({ message: 'User not found' });
user = await User.create({
email,
password: hashedPassword,
google_sheets_email: email, // Gmail used for Sheets
google_access_token: tokens.access_token,
google_refresh_token: tokens.refresh_token,
});
} else {
// Update user's Google tokens if they exist
await User.update({
google_access_token: tokens.access_token,
google_refresh_token: tokens.refresh_token,
}, { where: { email } });
} }
// Update the current user's Google Sheets email and tokens
user = await user.update({
google_sheets_email: email,
google_access_token: tokens.access_token,
google_refresh_token: tokens.refresh_token,
});
// List user's Google Sheets from their Google Drive // List user's Google Sheets from their Google Drive
const drive = google.drive({ version: 'v3', auth: oauth2Client }); const drive = google.drive({ version: 'v3', auth: oauth2Client });
const response = await drive.files.list({ const response = await drive.files.list({
@@ -243,7 +238,7 @@ router.get('/google/callback', requireSignIn, async (req, res) => {
res.json({ res.json({
message: 'Google authentication successful', message: 'Google authentication successful',
email: user.email, google_sheet_email: user.google_sheets_email,
jwtToken, jwtToken,
files files
}); });
@@ -252,7 +247,6 @@ router.get('/google/callback', requireSignIn, async (req, res) => {
} }
}); });
// Step 3: Get data from Google Sheets // Step 3: Get data from Google Sheets
router.post('/gsheets/data', requireSignIn, async (req, res) => { router.post('/gsheets/data', requireSignIn, async (req, res) => {
const { spreadsheetId } = req.body; const { spreadsheetId } = req.body;