Skip to content

Commit

Permalink
feat: allow interstitial callbacks to be async functions [breaking]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
julianlam committed Jan 22, 2021
1 parent d1f78cb commit 280285c
Showing 1 changed file with 29 additions and 35 deletions.
64 changes: 29 additions & 35 deletions src/controllers/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,15 @@ 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);
}

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;
Expand All @@ -168,43 +163,42 @@ 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 {
res.redirect(nconf.get('relative_path') + '/');
}
};

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);
return res.redirect(nconf.get('relative_path') + '/register/complete');
}
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');
}

if (req.session.registration.register === true) {
res.locals.processLogin = true;
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;

Object.keys(payload).forEach((prop) => {
if (typeof payload[prop] === 'boolean') {
payload[prop] = payload[prop] ? 1 : 0;
}
});
if (req.session.registration.register === true) {
res.locals.processLogin = true;
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;

Object.keys(payload).forEach((prop) => {
if (typeof payload[prop] === 'boolean') {
payload[prop] = payload[prop] ? 1 : 0;
}
});

await user.setUserFields(uid, payload);
done();
}
});
await user.setUserFields(uid, payload);
done();
}
});
};

Expand Down

0 comments on commit 280285c

Please sign in to comment.