minor changes
This commit is contained in:
parent
909b29dfb5
commit
af2e220116
30 changed files with 598 additions and 350 deletions
|
|
@ -9,34 +9,34 @@ primary_region = 'sea'
|
|||
[build]
|
||||
|
||||
[env]
|
||||
PORT = '8080'
|
||||
PORT = '8080'
|
||||
|
||||
[http_service]
|
||||
internal_port = 8080
|
||||
force_https = true
|
||||
auto_stop_machines = 'stop'
|
||||
auto_start_machines = true
|
||||
min_machines_running = 0
|
||||
processes = ['app']
|
||||
internal_port = 8080
|
||||
force_https = true
|
||||
auto_stop_machines = 'off'
|
||||
auto_start_machines = true
|
||||
min_machines_running = 0
|
||||
processes = ['app']
|
||||
|
||||
[[services]]
|
||||
protocol = 'tcp'
|
||||
internal_port = 8080
|
||||
protocol = 'tcp'
|
||||
internal_port = 8080
|
||||
|
||||
[[services.ports]]
|
||||
port = 80
|
||||
handlers = ['http']
|
||||
[[services.ports]]
|
||||
port = 80
|
||||
handlers = ['http']
|
||||
|
||||
[[services.ports]]
|
||||
port = 443
|
||||
handlers = ['tls', 'http']
|
||||
[[services.ports]]
|
||||
port = 443
|
||||
handlers = ['tls', 'http']
|
||||
|
||||
[[services.tcp_checks]]
|
||||
interval = '10s'
|
||||
timeout = '2s'
|
||||
grace_period = '5s'
|
||||
# [[services.tcp_checks]]
|
||||
# interval = '10s'
|
||||
# timeout = '2s'
|
||||
# grace_period = '5s'
|
||||
|
||||
[[vm]]
|
||||
memory = '1gb'
|
||||
cpu_kind = 'shared'
|
||||
cpus = 1
|
||||
memory = '1gb'
|
||||
cpu_kind = 'shared'
|
||||
cpus = 1
|
||||
|
|
|
|||
11
auth-service/old.env.development
Normal file
11
auth-service/old.env.development
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
NODE_ENV=development
|
||||
|
||||
GOOGLE_CLIENT_ID="485880105639-1in8tvb6ondnn198rasuj2d8ank06ntp.apps.googleusercontent.com"
|
||||
GOOGLE_CLIENT_SECRET="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"
|
||||
GOOGLE_CALLBACK_URL="http://localhost:8080/auth/google/callback"
|
||||
LOGIN_REDIRECT_URL="http://localhost:5173/"
|
||||
ACCEPTED_ORIGINS=http://localhost:3000,http://localhost:8081,http://localhost:3001,http://localhost:5173
|
||||
ASSIGNMENT_SERVICE_URL="http://localhost:8082"
|
||||
DB_USER_SERVICE_URL="http://localhost:3100/"
|
||||
AUTH_SESSION_KEY="f3f4d8e6b17a4b3abdc8e9a2c0457aaf91c0d5f6e3b7a9c8df624bd71ea35f42"
|
||||
PORT=8080
|
||||
|
|
@ -36,8 +36,11 @@ passport.use(
|
|||
|
||||
try {
|
||||
console.log("Sending request to external auth service...");
|
||||
console.log(
|
||||
`Request URL: ${process.env.ASSIGNMENT_SERVICE_URL}student/verify`
|
||||
);
|
||||
const response = await axios.post(
|
||||
`${process.env.ASSIGNMENT_SERVICE_URL}/student/verify`,
|
||||
`${process.env.ASSIGNMENT_SERVICE_URL}student/verify`,
|
||||
{
|
||||
qrNumber,
|
||||
password,
|
||||
|
|
@ -64,6 +67,7 @@ passport.use(
|
|||
|
||||
passport.serializeUser((user, done) => {
|
||||
console.log("Serializing user:", user);
|
||||
console.log(process.env.NODE_ENV)
|
||||
// done(null, user);
|
||||
done(null, {
|
||||
userId: user.qrcodenumber || user.userId,
|
||||
|
|
|
|||
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(
|
||||
|
|
|
|||
|
|
@ -6,19 +6,86 @@ const passport = require("passport");
|
|||
const passportSetup = require("./passport");
|
||||
const authRoute = require("./routes/auth");
|
||||
const apiRoute = require("./routes/api");
|
||||
const assignmentRoute = require("./routes/assignment");
|
||||
|
||||
const session = require("express-session");
|
||||
|
||||
const app = express();
|
||||
|
||||
// console.log("AUTH_URL:", process.env.AUTH_URL);
|
||||
app.use((req, res, next) => {
|
||||
console.log('Protocol before proxy:', req.protocol, 'Secure:', req.secure);
|
||||
next();
|
||||
});
|
||||
app.use((req, res, next) => {
|
||||
console.log('req.secure:', req.secure);
|
||||
console.log('x-forwarded-proto:', req.headers['x-forwarded-proto']);
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
app.set('trust proxy', true); // proxy magic that needs to happen
|
||||
|
||||
|
||||
// app.use((req, res, next) => {
|
||||
// console.log('Protocol after proxy:', req.protocol, 'Secure:', req.secure);
|
||||
// next();
|
||||
// });
|
||||
|
||||
|
||||
const allowedOrigins = process.env.ACCEPTED_ORIGINS.split(",");
|
||||
|
||||
const corsOptions = {
|
||||
// origin: function (origin, callback) {
|
||||
// if (!origin || allowedOrigins.includes(origin)) {
|
||||
// callback(null, origin); // allow the request
|
||||
// } else {
|
||||
// callback(new Error("Not allowed by CORS"));
|
||||
// }
|
||||
// },
|
||||
origin: "https://snake-byte.org", // Replace with your frontend URL
|
||||
// methods: ["GET", "POST", "OPTIONS"],
|
||||
// allowedHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
};
|
||||
|
||||
app.use(cors(corsOptions));
|
||||
|
||||
|
||||
// app.use((req, res, next) => {
|
||||
// console.log("Session:", req.session);
|
||||
// console.log("User:", req.user);
|
||||
// next();
|
||||
// });
|
||||
|
||||
// app.use((req, res, next) => {
|
||||
// res.cookie(
|
||||
// 'myTestCookie', 'helloWorld',
|
||||
// {
|
||||
// httpOnly: true,
|
||||
// secure: true, // Set to true if using HTTPS
|
||||
// sameSite: 'none', // Use 'none' for cross-origin requests
|
||||
// domain: 'jank-frontend.fly.dev', // Set the domain to allow cross-origin requests
|
||||
// maxAge: 24 * 60 * 60 * 1000, // 1 day
|
||||
// path: '/', // Set the path for the cookie
|
||||
// }
|
||||
// );
|
||||
// next();
|
||||
// });
|
||||
|
||||
|
||||
console.log("AUTH_URL:", process.env.AUTH_URL);
|
||||
const isProduction = process.env.NODE_ENV === "production";
|
||||
app.use(
|
||||
session({
|
||||
secret: process.env.AUTH_SESSION_KEY,
|
||||
resave: false,
|
||||
saveUninitialized: false,
|
||||
saveUninitialized: false, // true in development, false in production
|
||||
cookie: {
|
||||
httpOnly: true, // true in production for sec
|
||||
maxAge: 24 * 60 * 60 * 1000, // 1 day
|
||||
secure: true, //true // only true in production over HTTPS
|
||||
sameSite: 'none', // or 'none' if using cross-origin
|
||||
// domain: '', // Set the domain to allow cross-origin requests, or not?
|
||||
//keep production security settings below disable for the mean-time because we need to integrate redis session for cross-origin to work properly
|
||||
//sameSite: isProduction ? "none" : "lax", // or 'none' if using cross-origin
|
||||
//secure: isProduction, // only true in production over HTTPS
|
||||
|
|
@ -26,28 +93,36 @@ app.use(
|
|||
})
|
||||
);
|
||||
|
||||
// console.log("this is the session", session);
|
||||
// console.log("this is the cookie", session.cookie);
|
||||
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
|
||||
const allowedOrigins = process.env.ACCEPTED_ORIGINS.split(",");
|
||||
|
||||
const corsOptions = {
|
||||
origin: function (origin, callback) {
|
||||
if (!origin || allowedOrigins.includes(origin)) {
|
||||
callback(null, origin); // allow the request
|
||||
} else {
|
||||
callback(new Error("Not allowed by CORS"));
|
||||
}
|
||||
},
|
||||
methods: ["GET", "POST", "OPTIONS"],
|
||||
allowedHeaders: ["Content-Type", "Authorization"],
|
||||
credentials: true,
|
||||
};
|
||||
|
||||
app.use(cors(corsOptions));
|
||||
// app.use((req, res, next) => {
|
||||
// res.on("finish", () => {
|
||||
// console.log(`Response Status: ${res.statusCode}`);
|
||||
// console.log(`Response Headers:`, res.getHeaders());
|
||||
// });
|
||||
// next();
|
||||
// })
|
||||
|
||||
|
||||
// app.use((req, res, next) => {
|
||||
// res.on("finish", () => {
|
||||
// const headers = res.getHeaders();
|
||||
// console.log("Set-Cookie header:", headers["set-cookie"]);
|
||||
// });
|
||||
// next();
|
||||
// });
|
||||
|
||||
|
||||
app.use("/assignment", assignmentRoute);
|
||||
app.use("/api", apiRoute);
|
||||
app.use("/auth", authRoute);
|
||||
|
||||
const port = process.env.PORT || 8080;
|
||||
app.listen(port, () => console.log(`Listening on port ${port}...`));
|
||||
const port = 8080;
|
||||
console.log(`Listening on port ${port}...`);
|
||||
app.listen(port, '0.0.0.0');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue