From 35a2135b36b86fea968022dd47d409bb439428a7 Mon Sep 17 00:00:00 2001 From: BoHong Li Date: Wed, 26 Feb 2020 11:22:59 +0800 Subject: [PATCH] refactor: change errorBadRequest function signature to avoid parameter passing error Signed-off-by: BoHong Li --- lib/auth/email/index.js | 8 ++++---- lib/auth/ldap/index.js | 2 +- lib/history/index.js | 10 +++++----- lib/middleware/checkURIValid.js | 2 +- lib/response.js | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/auth/email/index.js b/lib/auth/email/index.js index 56ed17a1..4c130b9f 100644 --- a/lib/auth/email/index.js +++ b/lib/auth/email/index.js @@ -33,8 +33,8 @@ passport.use(new LocalStrategy({ if (config.allowEmailRegister) { emailAuth.post('/register', urlencodedParser, function (req, res, next) { - if (!req.body.email || !req.body.password) return response.errorBadRequest(res) - if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res) + if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res) + if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res) models.User.findOrCreate({ where: { email: req.body.email @@ -63,8 +63,8 @@ if (config.allowEmailRegister) { } emailAuth.post('/login', urlencodedParser, function (req, res, next) { - if (!req.body.email || !req.body.password) return response.errorBadRequest(res) - if (!validator.isEmail(req.body.email)) return response.errorBadRequest(res) + if (!req.body.email || !req.body.password) return response.errorBadRequest(req, res) + if (!validator.isEmail(req.body.email)) return response.errorBadRequest(req, res) setReturnToFromReferer(req) passport.authenticate('local', { successReturnToOrRedirect: config.serverURL + '/', diff --git a/lib/auth/ldap/index.js b/lib/auth/ldap/index.js index 2f3b9a11..348c0d76 100644 --- a/lib/auth/ldap/index.js +++ b/lib/auth/ldap/index.js @@ -81,7 +81,7 @@ passport.use(new LDAPStrategy({ })) ldapAuth.post('/auth/ldap', urlencodedParser, function (req, res, next) { - if (!req.body.username || !req.body.password) return response.errorBadRequest(res) + if (!req.body.username || !req.body.password) return response.errorBadRequest(req, res) setReturnToFromReferer(req) passport.authenticate('ldapauth', { successReturnToOrRedirect: config.serverURL + '/', diff --git a/lib/history/index.js b/lib/history/index.js index e38af7e4..139bd9f7 100644 --- a/lib/history/index.js +++ b/lib/history/index.js @@ -131,12 +131,12 @@ function historyPost (req, res) { if (req.isAuthenticated()) { var noteId = req.params.noteId if (!noteId) { - if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(res) + if (typeof req.body['history'] === 'undefined') return response.errorBadRequest(req, res) if (config.debug) { logger.info('SERVER received history from [' + req.user.id + ']: ' + req.body.history) } try { var history = JSON.parse(req.body.history) } catch (err) { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } if (Array.isArray(history)) { setHistory(req.user.id, history, function (err, count) { @@ -144,10 +144,10 @@ function historyPost (req, res) { res.end() }) } else { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } } else { - if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(res) + if (typeof req.body['pinned'] === 'undefined') return response.errorBadRequest(req, res) getHistory(req.user.id, function (err, history) { if (err) return response.errorInternalError(res) if (!history) return response.errorNotFound(req, res) @@ -159,7 +159,7 @@ function historyPost (req, res) { res.end() }) } else { - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } }) } diff --git a/lib/middleware/checkURIValid.js b/lib/middleware/checkURIValid.js index edb61da2..00f2341e 100644 --- a/lib/middleware/checkURIValid.js +++ b/lib/middleware/checkURIValid.js @@ -8,7 +8,7 @@ module.exports = function (req, res, next) { decodeURIComponent(req.path) } catch (err) { logger.error(err) - return response.errorBadRequest(res) + return response.errorBadRequest(req, res) } next() } diff --git a/lib/response.js b/lib/response.js index 8fdb36a3..66c063e6 100644 --- a/lib/response.js +++ b/lib/response.js @@ -41,7 +41,7 @@ function errorNotFound (req, res) { responseError(res, '404', 'Not Found', 'oops.') } -function errorBadRequest (res) { +function errorBadRequest (req, res) { responseError(res, '400', 'Bad Request', 'something not right.') }