feat: use robot instead of user
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { Router, Request, Response } from 'express';
|
import { Router, Request, Response } from 'express';
|
||||||
import User from '../models/User';
|
import User from '../models/User';
|
||||||
|
import Robot from '../models/Robot';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { hashPassword, comparePassword } from '../utils/auth';
|
import { hashPassword, comparePassword } from '../utils/auth';
|
||||||
import { requireSignIn } from '../middlewares/auth';
|
import { requireSignIn } from '../middlewares/auth';
|
||||||
@@ -187,10 +188,15 @@ router.get('/google', (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Step 2: Handle Google OAuth callback
|
// Step 2: Handle Google OAuth callback
|
||||||
router.get('/google/callback', requireSignIn, async (req, res) => {
|
router.post('/google/callback', requireSignIn, async (req, res) => {
|
||||||
const { code } = req.query;
|
const { code } = req.query;
|
||||||
|
const { robotId } = req.body;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (!robotId) {
|
||||||
|
return res.status(400).json({ message: 'Robot ID is required' });
|
||||||
|
}
|
||||||
|
|
||||||
// Get access and refresh tokens
|
// Get access and refresh tokens
|
||||||
if (typeof code !== 'string') {
|
if (typeof code !== 'string') {
|
||||||
return res.status(400).json({ message: 'Invalid code' });
|
return res.status(400).json({ message: 'Invalid code' });
|
||||||
@@ -213,8 +219,13 @@ router.get('/google/callback', requireSignIn, async (req, res) => {
|
|||||||
return res.status(400).json({ message: 'User not found' });
|
return res.status(400).json({ message: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the current user's Google Sheets email and tokens
|
let robot = await Robot.findOne({ where: { 'recording_meta.id': robotId } });
|
||||||
user = await user.update({
|
|
||||||
|
if (!robot) {
|
||||||
|
return res.status(400).json({ message: 'Robot not found' });
|
||||||
|
}
|
||||||
|
|
||||||
|
robot = await robot.update({
|
||||||
google_sheets_email: email,
|
google_sheets_email: email,
|
||||||
google_access_token: tokens.access_token,
|
google_access_token: tokens.access_token,
|
||||||
google_refresh_token: tokens.refresh_token,
|
google_refresh_token: tokens.refresh_token,
|
||||||
@@ -236,13 +247,14 @@ router.get('/google/callback', requireSignIn, async (req, res) => {
|
|||||||
const jwtToken = jwt.sign({ userId: user.id }, process.env.JWT_SECRET as string, { expiresIn: '12h' });
|
const jwtToken = jwt.sign({ userId: user.id }, process.env.JWT_SECRET as string, { expiresIn: '12h' });
|
||||||
res.cookie('token', jwtToken, { httpOnly: true });
|
res.cookie('token', jwtToken, { httpOnly: true });
|
||||||
|
|
||||||
|
res.redirect('http://localhost:3000');
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
message: 'Google authentication successful',
|
message: 'Google authentication successful',
|
||||||
google_sheet_email: user.google_sheets_email,
|
google_sheet_email: robot.google_sheets_email,
|
||||||
jwtToken,
|
jwtToken,
|
||||||
files
|
files
|
||||||
});
|
});
|
||||||
res.redirect('http://localhost:3000');
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
res.status(500).json({ message: `Google OAuth error: ${error.message}` });
|
res.status(500).json({ message: `Google OAuth error: ${error.message}` });
|
||||||
}
|
}
|
||||||
@@ -250,17 +262,23 @@ 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, robotId } = req.body;
|
||||||
const user = await User.findOne({ where: { id: req.user.id } });
|
const user = await User.findOne({ where: { id: req.user.id } });
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(400).json({ message: 'User not found' });
|
return res.status(400).json({ message: 'User not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const robot = await Robot.findOne({ where: { 'recording_meta.id': robotId } });
|
||||||
|
|
||||||
|
if (!robot) {
|
||||||
|
return res.status(400).json({ message: 'Robot not found' });
|
||||||
|
}
|
||||||
|
|
||||||
// Set Google OAuth credentials
|
// Set Google OAuth credentials
|
||||||
oauth2Client.setCredentials({
|
oauth2Client.setCredentials({
|
||||||
access_token: user.google_access_token,
|
access_token: robot.google_access_token,
|
||||||
refresh_token: user.google_refresh_token,
|
refresh_token: robot.google_refresh_token,
|
||||||
});
|
});
|
||||||
|
|
||||||
const sheets = google.sheets({ version: 'v4', auth: oauth2Client });
|
const sheets = google.sheets({ version: 'v4', auth: oauth2Client });
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import { requireSignIn } from '../middlewares/auth';
|
|||||||
import Robot from '../models/Robot';
|
import Robot from '../models/Robot';
|
||||||
import Run from '../models/Run';
|
import Run from '../models/Run';
|
||||||
import { BinaryOutputService } from '../storage/mino';
|
import { BinaryOutputService } from '../storage/mino';
|
||||||
import { workflowQueue } from '../worker';
|
// import { workflowQueue } from '../worker';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
@@ -282,16 +282,16 @@ router.put('/schedule/:id/', requireSignIn, async (req, res) => {
|
|||||||
const runId = uuid();
|
const runId = uuid();
|
||||||
const userId = req.user.id;
|
const userId = req.user.id;
|
||||||
|
|
||||||
await workflowQueue.add(
|
// await workflowQueue.add(
|
||||||
'run workflow',
|
// 'run workflow',
|
||||||
{ id, runId, userId },
|
// { id, runId, userId },
|
||||||
{
|
// {
|
||||||
repeat: {
|
// repeat: {
|
||||||
pattern: cronExpression,
|
// pattern: cronExpression,
|
||||||
tz: timezone
|
// tz: timezone
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
);
|
// );
|
||||||
|
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
message: 'success',
|
message: 'success',
|
||||||
|
|||||||
Reference in New Issue
Block a user