protect endpoint

This commit is contained in:
andrzej 2024-05-27 15:20:24 +02:00
parent 68f77317a3
commit c749adff34
2 changed files with 35 additions and 41 deletions

View File

@ -1,6 +1,6 @@
import pino from 'pino' import pino from "pino";
import path from 'path' import path from "path";
import { fileURLToPath } from 'url'; import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
@ -16,7 +16,7 @@ const __dirname = path.dirname(__filename);
// }) // })
export default pino( export default pino(
{ {
level: 'fatal', level: "info",
formatters: { formatters: {
level: (label) => { level: (label) => {
return { level: label.toUpperCase() }; return { level: label.toUpperCase() };
@ -25,4 +25,5 @@ export default pino(
timestamp: pino.stdTimeFunctions.isoTime, timestamp: pino.stdTimeFunctions.isoTime,
}, },
//pino.destination(`${__dirname}/app.log`) //pino.destination(`${__dirname}/app.log`)
); );

View File

@ -54,18 +54,12 @@ export const postEndpoints = (db, data) => {
}; };
const protectedEndpoint = (router, Entity, path, method, db, data) => { const protectedEndpoint = (router, Entity, path, method, db, data) => {
router.post(`/${Entity.name.toLowerCase()}/${path}`, async (req, res) => { router.post(
passport.authenticate( `/${Entity.name.toLowerCase()}/${path}`,
"jwt", passport.authenticate("jwt", { session: false }, (_, res) => {
{ session: false }, res.json({ message: "protected endpoint" });
async (err, user, info) => { }),
if (err) { async (req, res) => {
logger.error(err);
}
if (info !== undefined) {
logger.info(info.message);
res.status(401).send(info.message);
}
try { try {
logger.trace({ data: req.body }, "POST request received"); logger.trace({ data: req.body }, "POST request received");
const entity = new Entity(req.body); const entity = new Entity(req.body);
@ -84,5 +78,4 @@ const protectedEndpoint = (router, Entity, path, method, db, data) => {
} }
}, },
); );
});
}; };