Skip to content

Commit

Permalink
Move header logic out of try-catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
big213 committed May 26, 2021
1 parent 9a68fc4 commit 48bafaa
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions backend/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,30 @@ app.use(async function (req, res, next) {
} else if (req.headers.authorization) {
req.user = await validateToken(req.headers.authorization);
}

// handle origins -- only accepting string type origins.
const origin =
Array.isArray(allowedOrigins) && allowedOrigins.length
? typeof req.headers.origin === "string" &&
allowedOrigins.includes(req.headers.origin)
? req.headers.origin
: allowedOrigins[0]
: "*";

res.header("Access-Control-Allow-Origin", origin);
if (origin !== "*") {
res.header("Vary", "Origin");
}

res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"
);
res.header(
"Access-Control-Allow-Methods",
"PUT, POST, GET, DELETE, OPTIONS"
);
} catch (err) {
console.log(err);
}

// handle origins -- only accepting string type origins.
const origin =
Array.isArray(allowedOrigins) && allowedOrigins.length
? typeof req.headers.origin === "string" &&
allowedOrigins.includes(req.headers.origin)
? req.headers.origin
: allowedOrigins[0]
: "*";

res.header("Access-Control-Allow-Origin", origin);
if (origin !== "*") {
res.header("Vary", "Origin");
}

res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept, Authorization, Cache-Control"
);
res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, OPTIONS");

next();
});

Expand Down

0 comments on commit 48bafaa

Please sign in to comment.