mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 09:06:15 +01:00
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.
This commit is contained in:
@@ -140,7 +140,7 @@ authenticationController.registerComplete = function (req, res, next) {
|
|||||||
plugins.hooks.fire('filter:register.interstitial', {
|
plugins.hooks.fire('filter:register.interstitial', {
|
||||||
userData: req.session.registration,
|
userData: req.session.registration,
|
||||||
interstitials: [],
|
interstitials: [],
|
||||||
}, function (err, data) {
|
}, async (err, data) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
@@ -148,12 +148,7 @@ authenticationController.registerComplete = function (req, res, next) {
|
|||||||
var callbacks = data.interstitials.reduce(function (memo, cur) {
|
var callbacks = data.interstitials.reduce(function (memo, cur) {
|
||||||
if (cur.hasOwnProperty('callback') && typeof cur.callback === 'function') {
|
if (cur.hasOwnProperty('callback') && typeof cur.callback === 'function') {
|
||||||
req.body.files = req.files;
|
req.body.files = req.files;
|
||||||
memo.push(function (next) {
|
memo.push(cur.callback && cur.callback.constructor && cur.callback.constructor.name === 'AsyncFunction' ? cur.callback : util.promisify(cur.callback));
|
||||||
cur.callback(req.session.registration, req.body, function (err) {
|
|
||||||
// Pass error as second argument so all callbacks are executed
|
|
||||||
next(null, err);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return memo;
|
return memo;
|
||||||
@@ -168,6 +163,7 @@ authenticationController.registerComplete = function (req, res, next) {
|
|||||||
if (!err && data && data.message) {
|
if (!err && data && data.message) {
|
||||||
return res.redirect(nconf.get('relative_path') + '/?register=' + encodeURIComponent(data.message));
|
return res.redirect(nconf.get('relative_path') + '/?register=' + encodeURIComponent(data.message));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.session.returnTo) {
|
if (req.session.returnTo) {
|
||||||
res.redirect(nconf.get('relative_path') + req.session.returnTo);
|
res.redirect(nconf.get('relative_path') + req.session.returnTo);
|
||||||
} else {
|
} else {
|
||||||
@@ -175,36 +171,34 @@ authenticationController.registerComplete = function (req, res, next) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
async.parallel(callbacks, async function (_blank, err) {
|
const results = await Promise.allSettled(callbacks.map(async (cb) => {
|
||||||
if (err.length) {
|
await cb(req.session.registration, req.body);
|
||||||
err = err.filter(Boolean).map(err => err.message);
|
}));
|
||||||
}
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
if (err.length) {
|
if (req.session.registration.register === true) {
|
||||||
req.flash('errors', err);
|
res.locals.processLogin = true;
|
||||||
return res.redirect(nconf.get('relative_path') + '/register/complete');
|
registerAndLoginUserCallback(req, res, req.session.registration, done);
|
||||||
}
|
} else {
|
||||||
|
// Update user hash, clear registration data in session
|
||||||
|
const payload = req.session.registration;
|
||||||
|
const uid = payload.uid;
|
||||||
|
delete payload.uid;
|
||||||
|
delete payload.returnTo;
|
||||||
|
|
||||||
if (req.session.registration.register === true) {
|
Object.keys(payload).forEach((prop) => {
|
||||||
res.locals.processLogin = true;
|
if (typeof payload[prop] === 'boolean') {
|
||||||
registerAndLoginUserCallback(req, res, req.session.registration, done);
|
payload[prop] = payload[prop] ? 1 : 0;
|
||||||
} else {
|
}
|
||||||
// Update user hash, clear registration data in session
|
});
|
||||||
const payload = req.session.registration;
|
|
||||||
const uid = payload.uid;
|
|
||||||
delete payload.uid;
|
|
||||||
delete payload.returnTo;
|
|
||||||
|
|
||||||
Object.keys(payload).forEach((prop) => {
|
await user.setUserFields(uid, payload);
|
||||||
if (typeof payload[prop] === 'boolean') {
|
done();
|
||||||
payload[prop] = payload[prop] ? 1 : 0;
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await user.setUserFields(uid, payload);
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user