Skip to content

Commit

Permalink
Merge pull request #20 from LucaSpiritini/cors_options
Browse files Browse the repository at this point in the history
ADD: Cors options
  • Loading branch information
Mldamico committed Nov 9, 2022
2 parents a3cbb5b + a1b10dc commit b29265f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ require("dotenv").config();

const indexRouter = require("./routes/index");
const db = require("./database/models");
const { corsOptions } = require("./helpers/corsOptions");

const port = process.env.PORT || 3000;

const app = express();
app.use(cors());
app.use(cors(corsOptions));

app.use(logger("dev"));
app.use(express.json());
Expand Down
2 changes: 1 addition & 1 deletion controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ module.exports = {
const response = { balance, income, outcome };
endpointResponse({
res,
message: "Transaction removed successfully",
message: "Get balance successfully",
body: response,
});
} catch (error) {
Expand Down
20 changes: 20 additions & 0 deletions helpers/corsOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

const allowedOrigins = [
'http://127.0.0.1:5173',
'http://localhost:5173'
]

const corsOptions = {
origin: (origin, callback) => {

if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
credentials: true,
optionsSuccessStatus: 200
}

module.exports = { corsOptions }
1 change: 1 addition & 0 deletions helpers/tokensFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function authMiddleware(req, res, next) {
const bearerToken = authHeader.split(" ")[1];
try {
const payload = await verifyJwt(bearerToken);

req.body.id = payload.id;
req.body.username = payload.username;
return next();
Expand Down

0 comments on commit b29265f

Please sign in to comment.