From 6c74c7c524af6e8ab758540148365dc486773df3 Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Wed, 17 Sep 2025 22:02:44 +0200 Subject: [PATCH] Fix jwt-token --- server/routes/detectors.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/server/routes/detectors.js b/server/routes/detectors.js index 8e2c708..08a303c 100644 --- a/server/routes/detectors.js +++ b/server/routes/detectors.js @@ -156,7 +156,7 @@ async function handleHeartbeat(req, res) { console.log(`🚫 Heartbeat rejected from unapproved device ${deviceId}`); // Emit reminder notification to tenant room only - if (device.tenant_id) { + if (req.io && device.tenant_id) { req.io.to(`tenant_${device.tenant_id}`).emit('device_approval_reminder', { device_id: deviceId, device_key: key, @@ -198,7 +198,7 @@ async function handleHeartbeat(req, res) { const heartbeat = await Heartbeat.create(heartbeatRecord); // Emit real-time update via Socket.IO to tenant room only - if (device.tenant_id) { + if (req.io && device.tenant_id) { req.io.to(`tenant_${device.tenant_id}`).emit('device_heartbeat', { device_id: deviceId, device_key: key, @@ -266,7 +266,7 @@ async function handleDetection(req, res) { console.log(`🚫 Detection rejected from unapproved device ${detectionData.device_id}`); // Emit reminder notification to tenant room only - if (device.tenant_id) { + if (req.io && device.tenant_id) { req.io.to(`tenant_${device.tenant_id}`).emit('device_approval_reminder', { device_id: detectionData.device_id, timestamp: new Date().toISOString(), @@ -406,13 +406,15 @@ async function handleDetection(req, res) { }; // 🔒 SECURITY: Emit only to the tenant's room to prevent cross-tenant data leakage - if (device.tenant_id) { + if (req.io && device.tenant_id) { req.io.to(`tenant_${device.tenant_id}`).emit('drone_detection', detectionPayload); console.log(`🔒 Detection emitted to tenant room: tenant_${device.tenant_id}`); - } else { + } else if (req.io) { // Fallback for devices without tenant_id (legacy support) console.warn(`⚠️ Device ${device.id} has no tenant_id - using global broadcast (security risk)`); req.io.emit('drone_detection', detectionPayload); + } else { + console.warn(`⚠️ Socket.IO not available - detection will not be broadcast in real-time`); } // Process alerts asynchronously (from original)