Skip to content

Commit

Permalink
Resolved Session id undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ldco2016 committed Jul 23, 2024
1 parent e639baa commit c14205f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
42 changes: 36 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,57 @@ const cleanup = require("./queries/cleanup");

const app = express();

app.set("trust proxy", "127.0.0.1");
const allowedOrigins =
process.env.NODE_ENV === "production"
? ["https://pg-sql.com", "https://notes.pg-sql.com"]
: ["http://localhost:3000"];

app.set("trust proxy", process.env.NODE_ENV === "production" ? 1 : 0);
app.use(
cors({
origin:
process.env.NODE_ENV === "production"
? ["https://pg-sql.com", "https://notes.pg-sql.com"]
: ["http://localhost:3000"],
origin: function (origin, callback) {
if (!origin) {
return callback(null, true);
}
if (allowedOrigins.indexOf(origin) === -1) {
const msg =
"The CORS policy for this site does not allow access from the specified origin";
return callback(new Error(msg), false);
}
return callback(null, true);
},
credentials: true,
})
);

// app.use(
// cors({
// origin:
// process.env.NODE_ENV === "production"
// ? ["https://pg-sql.com", "https://notes.pg-sql.com"]
// : ["http://localhost:3000"],
// credentials: true,
// })
// );
app.use(express.json());
app.use(
cookieSession({
secure: process.env.NODE_ENV === "production",
keys: [keys.cookieKey],
sameSite: "Lax",
httpOnly: true,
domain: "pg-sql.com",
domain: process.env.NODE_ENV === "production" ? "pg-sql.com" : undefined,
})
);

app.use((req, res, next) => {
console.log("Session:", req.session);
if (!req.session) {
return next(new Error("Session is not set up properly"));
}
next();
});

app.post("/provision", require("./provision"));
app.post("/query", require("./query"));
app.post("/reset", require("./reset"));
Expand Down
9 changes: 9 additions & 0 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const touchLogin = require("./queries/touch-login");

module.exports = async (req, res) => {
const { id } = req.session;

console.log("Session:", id);
if (!id) {
return res.status(400).json({ error: "Session ID is missing" });
}
validateId(id);

if (!(await dbExists(id))) {
Expand All @@ -15,6 +20,7 @@ module.exports = async (req, res) => {
const client = await createClient(id);

try {
console.log("Query:", req.body.query);
const result = await client.query(req.body.query);

if (Array.isArray(result)) {
Expand All @@ -36,6 +42,9 @@ module.exports = async (req, res) => {
},
]);
}
} catch (err) {
console.error("Query Execution Error:", err);
res.status(500).json({ error: err.message });
} finally {
await client.end();
}
Expand Down

0 comments on commit c14205f

Please sign in to comment.