-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.js
More file actions
35 lines (31 loc) · 833 Bytes
/
Copy pathmiddleware.js
File metadata and controls
35 lines (31 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
let cors = (req, res, next) => {
const ACCESS_TYPE = "PUBLIC"
const ALLOWED_ORIGINS = ["http://localhost:3003", "http://localhost:3000"]
let origin = req.headers.origin
if (ACCESS_TYPE === "PUBLIC") {
if (origin !== undefined) {
res.setHeader("Access-Control-Allow-Origin", origin)
}
} else {
if (ALLOWED_ORIGINS.includes(origin)) {
res.setHeader("Access-Control-Allow-Origin", origin)
} else {
console.log({
origin,
message: "Origin not allowed",
status: "nope",
method: req.method,
})
}
}
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, PUT, DELETE",
"OPTIONS"
)
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization")
res.setHeader("Access-Control-Allow-Credentials", true)
res.setHeader("Vary", "Origin")
return next()
}
export { cors }