mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36: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);
|
||||
}
|
||||
});
|
||||
|
||||
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 () {
|
||||
var app = require('express').Router();
|
||||
|
||||
app.post('/', middleware.authenticate, middleware.isAdmin, async (req, res) => {
|
||||
helpers.checkRequired(['username'], req, res);
|
||||
// if (!utils.checkRequired(['username'], req, res)) {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
app.post('/', middleware.checkRequired.bind(null, ['username']), middleware.authenticate, middleware.isAdmin, async (req, res) => {
|
||||
const uid = await users.create(req.body);
|
||||
helpers.formatApiResponse(200, res, {
|
||||
uid: uid,
|
||||
|
||||
Reference in New Issue
Block a user