From 4a09dfbd08253115c342a9e829c4e6940cecb8cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Zanghelini?= Date: Sun, 2 Jul 2017 15:37:23 -0300 Subject: [PATCH] Moved all error handling into helpers function --- src/controllers/authentication.js | 27 +++++---------------------- src/controllers/helpers.js | 4 ++++ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 2d74c46794..9a2f47f75b 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -202,32 +202,21 @@ authenticationController.login = function (req, res, next) { continueLogin(req, res, next); } else { var err = '[[error:wrong-login-type-' + loginWith + ']]'; - - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err, 500); - } - res.status(500).send(err); + helpers.noScriptErrors(req, res, err, 500); } }; function continueLogin(req, res, next) { passport.authenticate('local', function (err, userData, info) { if (err) { - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err.message, 403); - } - return res.status(403).send(err.message); + return helpers.noScriptErrors(req, res, err.message, 403); } if (!userData) { if (typeof info === 'object') { info = '[[error:invalid-username-or-password]]'; } - - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, info, 403); - } - return res.status(403).send(info); + return helpers.noScriptErrors(req, res, info, 403); } var passwordExpiry = userData.passwordExpiry !== undefined ? parseInt(userData.passwordExpiry, 10) : null; @@ -247,10 +236,7 @@ function continueLogin(req, res, next) { req.session.passwordExpired = true; user.reset.generate(userData.uid, function (err, code) { if (err) { - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err.message, 403); - } - return res.status(403).send(err.message); + return helpers.noScriptErrors(req, res, err.message, 403); } res.status(200).send(nconf.get('relative_path') + '/reset/' + code); @@ -258,10 +244,7 @@ function continueLogin(req, res, next) { } else { authenticationController.doLogin(req, userData.uid, function (err) { if (err) { - if (req.body.noscript === 'true') { - return helpers.noScriptErrors(req, res, err.message, 403); - } - return res.status(403).send(err.message); + return helpers.noScriptErrors(req, res, err.message, 403); } var next; diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 3057aa5265..cbc9f35110 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -15,6 +15,10 @@ var middleware = require('../middleware'); var helpers = module.exports; helpers.noScriptErrors = function (req, res, error, httpStatus) { + if (req.body.noscript === 'false') { + return res.status(httpStatus).send(error); + } + var middleware = require('../middleware'); var httpStatusString = httpStatus.toString(); middleware.buildHeader(req, res, function () {