- Replace Socket.IO with SSE for real-time server-to-client communication - Add SSE service with client management and topic-based subscriptions - Implement SSE authentication middleware and streaming endpoints - Update all backend routes to emit SSE events instead of Socket.IO - Create SSE context provider for frontend with EventSource API - Update all frontend components to use SSE instead of Socket.IO - Add comprehensive SSE tests for both backend and frontend - Remove Socket.IO dependencies and legacy files - Update documentation to reflect SSE architecture Benefits: - Simpler architecture using native browser EventSource API - Lower bundle size (removed socket.io-client dependency) - Better compatibility with reverse proxies and load balancers - Reduced resource usage for Raspberry Pi deployment - Standard HTTP-based real-time communication 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const jwt = require("jsonwebtoken");
|
|
|
|
/**
|
|
* SSE Authentication Middleware
|
|
* Supports token from query string (for SSE connections) or Authorization header
|
|
*/
|
|
module.exports = function (req, res, next) {
|
|
let token;
|
|
|
|
// Try to get token from query string (for SSE EventSource connections)
|
|
if (req.query.token) {
|
|
token = req.query.token;
|
|
}
|
|
// Try to get token from Authorization header (Bearer token)
|
|
else if (req.headers.authorization && req.headers.authorization.startsWith("Bearer ")) {
|
|
token = req.headers.authorization.substring(7);
|
|
}
|
|
// Try to get token from x-auth-token header (legacy support)
|
|
else if (req.header("x-auth-token")) {
|
|
token = req.header("x-auth-token");
|
|
}
|
|
|
|
// Check if no token found
|
|
if (!token) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
msg: "No token, authorization denied"
|
|
});
|
|
}
|
|
|
|
// Verify token
|
|
try {
|
|
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
|
req.user = decoded.user;
|
|
next();
|
|
} catch (err) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
msg: "Token is not valid"
|
|
});
|
|
}
|
|
};
|