microservices/auth-service/routes/assignment.js

33 lines
961 B
JavaScript
Raw Normal View History

2025-08-25 14:23:55 -07:00
// routes/assignment.js
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const router = express.Router();
// Middleware to check authentication
function isAuthenticated(req, res, next) {
if (req.isAuthenticated && req.isAuthenticated()) {
return next();
} else {
return res.status(401).json({ error: "Not authenticated, visit /login" });
}
}
// Proxy configuration
const proxy = createProxyMiddleware({
target: "http://assignment-service.internal:8080",
changeOrigin: true,
pathRewrite: {
"^/assignment": "", // remove `/assignment` prefix when forwarding
},
onProxyReq(proxyReq, req, res) {
// Optional: log or modify headers
console.log(`Proxying ${req.method} request to ${proxyReq.protocol}//${proxyReq.host}${proxyReq.path}`);
},
});
// Apply both middleware
router.use(isAuthenticated, proxy);
module.exports = router;