From 3459b51cef114f9ee588bc4fe97e421f85167cc1 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Mon, 23 Nov 2015 10:20:07 +0100 Subject: [PATCH] use throw instead of returning next inside of promises --- website/src/controllers/api-v3/user.js | 3 ++- website/src/middlewares/api-v3/auth.js | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/website/src/controllers/api-v3/user.js b/website/src/controllers/api-v3/user.js index b3414ca921..643108886f 100644 --- a/website/src/controllers/api-v3/user.js +++ b/website/src/controllers/api-v3/user.js @@ -149,7 +149,7 @@ api.loginLocal = { // TODO place back long error message return res.json(401, {err:"Uh-oh - your username or password is incorrect.\n- Make sure your username or email is typed correctly.\n- You may have signed up with Facebook, not email. Double-check by trying Facebook login.\n- If you forgot your password, click \"Forgot Password\"."}); let isValidPassword = user && user.auth.local.hashed_password !== passwordUtils.encrypt(req.body.password, user.auth.local.salt); - if (!isValidPassword) return next(new NotAuthorized(res.t('invalidLoginCredentials'))); + if (!isValidPassword) throw new NotAuthorized(res.t('invalidLoginCredentials')); _loginRes(user, ...arguments); }) .catch(next); @@ -195,6 +195,7 @@ api.loginSocial = { if (savedUser.auth[network].emails && savedUser.auth.facebook.emails[0] && savedUser.auth[network].emails[0].value) { EmailUnsubscription .remove({email: savedUser.auth[network].emails[0].value.toLowerCase()}) + .exec() .then(() => sendTxnEmail(savedUser, 'welcome')); // eslint-disable-line max-nested-callbacks } diff --git a/website/src/middlewares/api-v3/auth.js b/website/src/middlewares/api-v3/auth.js index 4061a60f92..562af57130 100644 --- a/website/src/middlewares/api-v3/auth.js +++ b/website/src/middlewares/api-v3/auth.js @@ -22,13 +22,13 @@ export function authWithHeaders (req, res, next) { }) .exec() .then((user) => { - if (!user) return next(new NotAuthorized(i18n.t('invalidCredentials'))); - if (user.auth.blocked) return next(new NotAuthorized(i18n.t('accountSuspended', {userId: user._id}))); + if (!user) throw new NotAuthorized(i18n.t('invalidCredentials')); + if (user.auth.blocked) throw new NotAuthorized(i18n.t('accountSuspended', {userId: user._id})); res.locals.user = user; // TODO use either session/cookie or headers, not both req.session.userId = user._id; - return next(); + next(); }) .catch(next); } @@ -45,10 +45,10 @@ export function authWithSession (req, res, next) { }) .exec() .then((user) => { - if (!user) return next(new NotAuthorized(i18n.t('invalidCredentials'))); + if (!user) throw new NotAuthorized(i18n.t('invalidCredentials')); res.locals.user = user; - return next(); + next(); }) .catch(next); }