Skip to content

Commit 5a0a5e2

Browse files
committed
Take in more than 1 client urls for cors
1 parent b51ad23 commit 5a0a5e2

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CLIENT_URL=http://localhost:5173
1+
CLIENT_URLS=http://localhost:5173 # csv of supported client urls
22
EMAIL_PASS=very-secret-email-password
33
EMAIL_REPORT=[email protected]
44

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ const nodemailer = require("nodemailer");
55
const bodyparser = require("body-parser");
66
const dotenv = require("dotenv").config();
77

8-
const { PORT, CLIENT_URL, EMAIL_PASS, EMAIL_USER, EMAIL_REPORT } = process.env;
8+
const { PORT, CLIENT_URLS, EMAIL_PASS, EMAIL_USER, EMAIL_REPORT } = process.env;
9+
10+
const whitelist = CLIENT_URLS.split(",");
911

1012
app.use(express.json());
1113
app.use(bodyparser.json());
1214
app.use(
1315
cors({
14-
origin: [CLIENT_URL],
16+
origin: function (origin, callback) {
17+
if (whitelist.indexOf(origin) !== -1) {
18+
callback(null, true);
19+
} else {
20+
callback(new Error("Not allowed by CORS"));
21+
}
22+
},
1523
})
1624
);
1725

0 commit comments

Comments
 (0)