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

View File

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