forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcors.js
More file actions
22 lines (20 loc) · 685 Bytes
/
Copy pathcors.js
File metadata and controls
22 lines (20 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const cors = require('cors');
const AppError = require('../errors/AppError');
function buildCorsMiddleware(allowedOrigins) {
return cors({
origin(origin, callback) {
if (!origin || allowedOrigins.includes(origin)) return callback(null, true);
callback(new AppError(
'FORBIDDEN',
`Origin '${origin}' is not allowed. Allowed origins: ${allowedOrigins.join(', ')}`,
403,
{ origin, allowed_origins: allowedOrigins },
));
},
credentials: true,
methods: ['GET', 'POST', 'DELETE', 'PATCH', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
maxAge: 86400,
});
}
module.exports = buildCorsMiddleware;