use throw instead of returning next inside of promises

This commit is contained in:
Matteo Pagliazzi
2015-11-23 10:20:07 +01:00
parent 6d238a0770
commit 3459b51cef
2 changed files with 7 additions and 6 deletions

View File

@@ -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
}

View File

@@ -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);
}