Skip to content

Commit

Permalink
feat(backend): Integrate CORS policy
Browse files Browse the repository at this point in the history
  • Loading branch information
SaxenaShourya committed May 20, 2024
1 parent e889ea0 commit 78ff35e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());

// CORS Configuration
app.options("*", function (req, res, next) {
res.setHeader(
"Access-Control-Allow-Origin",
"https://spend-smart-dev.vercel.app"
);
res.setHeader("Access-Control-Allow-credentials", true);
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
next();
});
app.use(function (req, res, next) {
const origin = req.header.origin;
const allowedOrigins = ["https://spend-smart-dev.vercel.app"];
if (allowedOrigins.includes(origin)) {
next();
} else {
res.status(404).json({ error: "Blocked by CORS!" });
}
});

// Routes
app.use("/api/v1/users", userRoutes);
app.use("/api/v1/incomes", authenticateUser, incomeRoutes);
Expand Down

0 comments on commit 78ff35e

Please sign in to comment.