mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: added checkRequired middleware for API calls
This commit is contained in:
@@ -237,3 +237,14 @@ middleware.validateAuth = helpers.try(async function validateAuth(req, res, next
|
|||||||
next(err);
|
next(err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
middleware.checkRequired = function (fields, req, res, next) {
|
||||||
|
// Used in API calls to ensure that necessary parameters/data values are present
|
||||||
|
const missing = fields.filter(field => !req.body.hasOwnProperty(field));
|
||||||
|
|
||||||
|
if (!missing.length) {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
controllers.helpers.formatApiResponse(400, res, new Error('Required parameters were missing from this API call: ' + missing.join(', ')));
|
||||||
|
};
|
||||||
|
|||||||
@@ -15,12 +15,7 @@ const helpers = require('../../controllers/helpers');
|
|||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
var app = require('express').Router();
|
var app = require('express').Router();
|
||||||
|
|
||||||
app.post('/', middleware.authenticate, middleware.isAdmin, async (req, res) => {
|
app.post('/', middleware.checkRequired.bind(null, ['username']), middleware.authenticate, middleware.isAdmin, async (req, res) => {
|
||||||
helpers.checkRequired(['username'], req, res);
|
|
||||||
// if (!utils.checkRequired(['username'], req, res)) {
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
const uid = await users.create(req.body);
|
const uid = await users.create(req.body);
|
||||||
helpers.formatApiResponse(200, res, {
|
helpers.formatApiResponse(200, res, {
|
||||||
uid: uid,
|
uid: uid,
|
||||||
|
|||||||
Reference in New Issue
Block a user