Skip to content

Commit

Permalink
fix: bug where middlewares seemingly ran in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Oct 8, 2020
1 parent 6096f74 commit 549ca11
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/middleware/user.js
Expand Up @@ -3,6 +3,7 @@
const nconf = require('nconf');
const winston = require('winston');
const passport = require('passport');
const util = require('util');

const meta = require('../meta');
const user = require('../user');
Expand Down Expand Up @@ -30,6 +31,8 @@ const passportAuthenticateAsync = function (req, res) {

module.exports = function (middleware) {
async function authenticate(req, res) {
const loginAsync = util.promisify(req.login).bind(req);

if (req.loggedIn) {
return true;
} else if (req.headers.hasOwnProperty('authorization')) {
Expand All @@ -38,30 +41,24 @@ module.exports = function (middleware) {

// If the token received was a master token, a _uid must also be present for all calls
if (user.hasOwnProperty('uid')) {
req.login(user, async function (err) {
if (err) { throw new Error(err); }

await controllers.authentication.onSuccessfulLogin(req, user.uid);
req.uid = user.uid;
req.loggedIn = req.uid > 0;
return true;
});
await loginAsync(user);
await controllers.authentication.onSuccessfulLogin(req, user.uid);
req.uid = user.uid;
req.loggedIn = req.uid > 0;
return true;
} else if (user.hasOwnProperty('master') && user.master === true) {
if (req.body.hasOwnProperty('_uid') || req.query.hasOwnProperty('_uid')) {
user.uid = req.body._uid || req.query._uid;
delete user.master;

req.login(user, async function (err) {
if (err) { throw new Error(err); }

await controllers.authentication.onSuccessfulLogin(req, user.uid);
req.uid = user.uid;
req.loggedIn = req.uid > 0;
return true;
});
} else {
throw new Error('A master token was received without a corresponding `_uid` in the request body');
await loginAsync(user);
await controllers.authentication.onSuccessfulLogin(req, user.uid);
req.uid = user.uid;
req.loggedIn = req.uid > 0;
return true;
}

throw new Error('A master token was received without a corresponding `_uid` in the request body');
} else {
winston.warn('[api/authenticate] Unable to find user after verifying token');
return true;
Expand Down

0 comments on commit 549ca11

Please sign in to comment.