feat: allow interstitial callbacks to be async functions [breaking]

This change is breaking in the sense that if you have written
interstitial callbacks before that are async functions _with_ a
callback, those are no longer allowed. You will not need to call
next() as that argument will no longer be passed in to async
functions.
v1.18.x
Julian Lam 4 years ago
parent d1f78cb8c0
commit 280285cda9

@ -140,7 +140,7 @@ authenticationController.registerComplete = function (req, res, next) {
plugins.hooks.fire('filter:register.interstitial', {
userData: req.session.registration,
interstitials: [],
}, function (err, data) {
}, async (err, data) => {
if (err) {
return next(err);
}
@ -148,12 +148,7 @@ authenticationController.registerComplete = function (req, res, next) {
var callbacks = data.interstitials.reduce(function (memo, cur) {
if (cur.hasOwnProperty('callback') && typeof cur.callback === 'function') {
req.body.files = req.files;
memo.push(function (next) {
cur.callback(req.session.registration, req.body, function (err) {
// Pass error as second argument so all callbacks are executed
next(null, err);
});
});
memo.push(cur.callback && cur.callback.constructor && cur.callback.constructor.name === 'AsyncFunction' ? cur.callback : util.promisify(cur.callback));
}
return memo;
@ -168,6 +163,7 @@ authenticationController.registerComplete = function (req, res, next) {
if (!err && data && data.message) {
return res.redirect(nconf.get('relative_path') + '/?register=' + encodeURIComponent(data.message));
}
if (req.session.returnTo) {
res.redirect(nconf.get('relative_path') + req.session.returnTo);
} else {
@ -175,13 +171,12 @@ authenticationController.registerComplete = function (req, res, next) {
}
};
async.parallel(callbacks, async function (_blank, err) {
if (err.length) {
err = err.filter(Boolean).map(err => err.message);
}
if (err.length) {
req.flash('errors', err);
const results = await Promise.allSettled(callbacks.map(async (cb) => {
await cb(req.session.registration, req.body);
}));
const errors = results.map(result => result.reason && result.reason.message).filter(Boolean);
if (errors.length) {
req.flash('errors', errors);
return res.redirect(nconf.get('relative_path') + '/register/complete');
}
@ -205,7 +200,6 @@ authenticationController.registerComplete = function (req, res, next) {
done();
}
});
});
};
authenticationController.registerAbort = function (req, res) {

Loading…
Cancel
Save