protect endpoint
This commit is contained in:
parent
68f77317a3
commit
c749adff34
25
logger.mjs
25
logger.mjs
|
@ -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);
|
||||
|
||||
|
@ -15,14 +15,15 @@ const __dirname = path.dirname(__filename);
|
|||
// }]
|
||||
// })
|
||||
export default pino(
|
||||
{
|
||||
level: 'fatal',
|
||||
formatters: {
|
||||
level: (label) => {
|
||||
return { level: label.toUpperCase() };
|
||||
},
|
||||
{
|
||||
level: "info",
|
||||
formatters: {
|
||||
level: (label) => {
|
||||
return { level: label.toUpperCase() };
|
||||
},
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
},
|
||||
//pino.destination(`${__dirname}/app.log`)
|
||||
);
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
},
|
||||
//pino.destination(`${__dirname}/app.log`)
|
||||
);
|
||||
|
||||
|
|
|
@ -54,35 +54,28 @@ 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);
|
||||
}
|
||||
try {
|
||||
logger.trace({ data: req.body }, "POST request received");
|
||||
const entity = new Entity(req.body);
|
||||
await entity[method](db, data);
|
||||
res.sendStatus(200);
|
||||
data.init();
|
||||
return;
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
if (error instanceof TypeError) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
res.sendStatus(500);
|
||||
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);
|
||||
await entity[method](db, data);
|
||||
res.sendStatus(200);
|
||||
data.init();
|
||||
return;
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
if (error instanceof TypeError) {
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
},
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue