From 6b6a7d4b8a78ba5fa309e056db78a3fd74f52442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 5 Aug 2021 12:52:07 -0400 Subject: [PATCH] refactor: remove waterfall --- src/controllers/authentication.js | 40 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js index 1a16bebeec..18fe533d96 100644 --- a/src/controllers/authentication.js +++ b/src/controllers/authentication.js @@ -248,31 +248,29 @@ authenticationController.login = async (req, res, next) => { const loginWith = meta.config.allowLoginWith || 'username-email'; req.body.username = req.body.username.trim(); - - plugins.hooks.fire('filter:login.check', { req: req, res: res, userData: req.body }, (err) => { - if (err) { - return (res.locals.noScriptErrors || helpers.noScriptErrors)(req, res, err.message, 403); + const errorHandler = res.locals.noScriptErrors || helpers.noScriptErrors; + try { + await plugins.hooks.fire('filter:login.check', { req: req, res: res, userData: req.body }); + } catch (err) { + return errorHandler(req, res, err.message, 403); + } + try { + const isEmailLogin = loginWith.includes('email') && req.body.username && utils.isEmailValid(req.body.username); + const isUsernameLogin = loginWith.includes('username') && !validator.isEmail(req.body.username); + if (isEmailLogin) { + const username = await user.getUsernameByEmail(req.body.username); + if (username !== '[[global:guest]]') { + req.body.username = username; + } } - if (req.body.username && utils.isEmailValid(req.body.username) && loginWith.includes('email')) { - async.waterfall([ - function (next) { - user.getUsernameByEmail(req.body.username, next); - }, - function (username, next) { - if (username !== '[[global:guest]]') { - req.body.username = username; - } - - (res.locals.continueLogin || continueLogin)(strategy, req, res, next); - }, - ], next); - } else if (loginWith.includes('username') && !validator.isEmail(req.body.username)) { + if (isEmailLogin || isUsernameLogin) { (res.locals.continueLogin || continueLogin)(strategy, req, res, next); } else { - err = `[[error:wrong-login-type-${loginWith}]]`; - (res.locals.noScriptErrors || helpers.noScriptErrors)(req, res, err, 400); + errorHandler(req, res, `[[error:wrong-login-type-${loginWith}]]`, 400); } - }); + } catch (err) { + return errorHandler(req, res, err.message, 500); + } }; function continueLogin(strategy, req, res, next) {