fix(writeapi): authenticate middleware logic to work better with await

v1.18.x
Julian Lam 5 years ago
parent f6433ef2c5
commit fd67355b03

@ -16,14 +16,25 @@ const controllers = {
authentication: require('../controllers/authentication'), authentication: require('../controllers/authentication'),
}; };
const passportAuthenticateAsync = function (req, res) {
return new Promise((resolve, reject) => {
passport.authenticate('bearer', { session: false }, (err, user) => {
if (err) {
reject(err);
} else {
resolve(user);
}
})(req, res);
});
};
module.exports = function (middleware) { module.exports = function (middleware) {
async function authenticate(req, res) { async function authenticate(req, res) {
if (req.loggedIn) { if (req.loggedIn) {
return true; return true;
} else if (req.headers.hasOwnProperty('authorization')) { } else if (req.headers.hasOwnProperty('authorization')) {
passport.authenticate('bearer', { session: false }, function (err, user) { const user = await passportAuthenticateAsync(req, res);
if (err) { throw new Error(err); } if (!user) { return true; }
if (!user) { return false; }
// If the token received was a master token, a _uid must also be present for all calls // If the token received was a master token, a _uid must also be present for all calls
if (user.hasOwnProperty('uid')) { if (user.hasOwnProperty('uid')) {
@ -53,9 +64,8 @@ module.exports = function (middleware) {
} }
} else { } else {
winston.warn('[api/authenticate] Unable to find user after verifying token'); winston.warn('[api/authenticate] Unable to find user after verifying token');
return false; return true;
} }
})(req, res);
} }
await plugins.fireHook('response:middleware.authenticate', { await plugins.fireHook('response:middleware.authenticate', {

Loading…
Cancel
Save