Skip to content

Commit

Permalink
Refactor CORS origin check in app.ts and app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
SDE-ADNAN committed Dec 29, 2023
1 parent bbcda48 commit 51fb142
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ dotenv.config({
path: "./ENV/config.env"
})

// app.use(cors({
// origin: ['https://adnans-react-typescript-todo.netlify.app'], // Replace with your frontend domain
// methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// }));
const allowedOrigins:String[] = ['https://adnans-react-typescript-todo.netlify.app',''];

app.use(cors({
origin: ['https://adnans-react-typescript-todo.netlify.app'], // Replace with your frontend domain
origin: function (origin: string | undefined, callback) {
// Check if the request origin is allowed
if (allowedOrigins.includes(origin as string)) {
callback(null, true); // Allow the request
} else {
callback(new Error('Not allowed by CORS')); // Block the request
}
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
}));

Expand Down
15 changes: 14 additions & 1 deletion dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ const app = (0, express_1.default)();
dotenv_1.default.config({
path: "./ENV/config.env"
});
// app.use(cors({
// origin: ['https://adnans-react-typescript-todo.netlify.app'], // Replace with your frontend domain
// methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// }));
const allowedOrigins = ['https://adnans-react-typescript-todo.netlify.app', ''];
app.use((0, cors_1.default)({
origin: ['https://adnans-react-typescript-todo.netlify.app'],
origin: function (origin, callback) {
// Check if the request origin is allowed
if (allowedOrigins.includes(origin)) {
callback(null, true); // Allow the request
}
else {
callback(new Error('Not allowed by CORS')); // Block the request
}
},
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
}));
app.use((0, express_session_1.default)({
Expand Down

0 comments on commit 51fb142

Please sign in to comment.