fix: store robotId in state
This commit is contained in:
@@ -174,6 +174,10 @@ const oauth2Client = new google.auth.OAuth2(
|
|||||||
|
|
||||||
// Step 1: Redirect to Google for authentication
|
// Step 1: Redirect to Google for authentication
|
||||||
router.get('/google', (req, res) => {
|
router.get('/google', (req, res) => {
|
||||||
|
const { robotId } = req.query;
|
||||||
|
if (!robotId) {
|
||||||
|
return res.status(400).json({ message: 'Robot ID is required' });
|
||||||
|
}
|
||||||
const scopes = [
|
const scopes = [
|
||||||
'https://www.googleapis.com/auth/spreadsheets',
|
'https://www.googleapis.com/auth/spreadsheets',
|
||||||
'https://www.googleapis.com/auth/userinfo.email',
|
'https://www.googleapis.com/auth/userinfo.email',
|
||||||
@@ -183,18 +187,21 @@ router.get('/google', (req, res) => {
|
|||||||
access_type: 'offline',
|
access_type: 'offline',
|
||||||
prompt: 'consent', // Ensures you get a refresh token on first login
|
prompt: 'consent', // Ensures you get a refresh token on first login
|
||||||
scope: scopes,
|
scope: scopes,
|
||||||
|
state: robotId.toString(),
|
||||||
});
|
});
|
||||||
res.redirect(url);
|
res.redirect(url);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Step 2: Handle Google OAuth callback
|
// Step 2: Handle Google OAuth callback
|
||||||
router.get('/google/callback', requireSignIn, async (req, res) => {
|
router.get('/google/callback', requireSignIn, async (req, res) => {
|
||||||
const { code, robotId } = req.query;
|
const { code, state } = req.query;
|
||||||
try {
|
try {
|
||||||
if (!robotId) {
|
if (!state) {
|
||||||
return res.status(400).json({ message: 'Robot ID is required' });
|
return res.status(400).json({ message: 'Robot ID is required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const robotId = state
|
||||||
|
|
||||||
// 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' });
|
||||||
|
|||||||
Reference in New Issue
Block a user