minor changes
This commit is contained in:
parent
909b29dfb5
commit
af2e220116
30 changed files with 598 additions and 350 deletions
32
auth-service/routes/assignment.js
Normal file
32
auth-service/routes/assignment.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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;
|
||||
|
|
@ -6,6 +6,7 @@ const express = require("express");
|
|||
|
||||
const bodyParser = require("body-parser");
|
||||
|
||||
|
||||
auth.use(express.json());
|
||||
auth.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
|
|
@ -20,7 +21,7 @@ auth.get(
|
|||
async (req, res) => {
|
||||
console.log("Google callback endpoint hit");
|
||||
if (req.user) {
|
||||
console.log(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`);
|
||||
// console.log(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`);
|
||||
axios
|
||||
.post(`${process.env.DB_USER_SERVICE_URL}instructor/register-user`, {
|
||||
user: req.user,
|
||||
|
|
@ -35,8 +36,25 @@ auth.get(
|
|||
console.error("Login error:", err);
|
||||
return res.status(500).send("Login failed");
|
||||
}
|
||||
return res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
// Force session save before redirect
|
||||
req.session.save((err) => {
|
||||
if (err) {
|
||||
console.error("Session save error:", err);
|
||||
return res.status(500).send("Session save failed");
|
||||
}
|
||||
console.log("Session saved successfully");
|
||||
return res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
});
|
||||
console.log("User logged in successfully:", req.session);
|
||||
});
|
||||
|
||||
// req.login(req.user, (err) => {
|
||||
// if (err) {
|
||||
// console.error("Login error:", err);
|
||||
// return res.status(500).send("Login failed");
|
||||
// }
|
||||
// return res.redirect(process.env.LOGIN_REDIRECT_URL);
|
||||
// });
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error registering user:", error.message);
|
||||
|
|
@ -67,6 +85,19 @@ auth.get("/login/failed", (req, res) => {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
// Set a test cookie
|
||||
auth.get("/test-cookie", (req, res) => {
|
||||
res.cookie("test-session", "123", {
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
sameSite: "none",
|
||||
domain: "snake-byte.org", // Set the domain to allow cross-origin requests
|
||||
});
|
||||
res.send("Cookie set");
|
||||
});
|
||||
|
||||
|
||||
auth.get("/google", passport.authenticate("google", ["profile", "email"]));
|
||||
|
||||
auth.post(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue