Skip to content

Node.js based forum software built for the modern web

refactor: remove async from isPasswordValid, function is sync

Barış Soner Uşaklı22f8011686ce
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index 968209f7d0..4a13a4ce57 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -1,80 +1,25 @@
'use strict';
-var async = require('async');
-var winston = require('winston');
-var passport = require('passport');
-var nconf = require('nconf');
-var validator = require('validator');
-var _ = require('lodash');
-
-var db = require('../database');
-var meta = require('../meta');
-var user = require('../user');
-var plugins = require('../plugins');
-var utils = require('../utils');
-var translator = require('../translator');
-var helpers = require('./helpers');
-var middleware = require('../middleware');
-var privileges = require('../privileges');
-var sockets = require('../socket.io');
-
-var authenticationController = module.exports;
-
-authenticationController.register = function (req, res) {
- var registrationType = meta.config.registrationType || 'normal';
-
- if (registrationType === 'disabled') {
- return res.sendStatus(403);
- }
-
- var userData = req.body;
-
- async.waterfall([
- function (next) {
- if (registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
- user.verifyInvitation(userData, next);
- } else {
- next();
- }
- },
- function (next) {
- if (!userData.email) {
- return next(new Error('[[error:invalid-email]]'));
- }
-
- if (!userData.username || userData.username.length < meta.config.minimumUsernameLength || utils.slugify(userData.username).length < meta.config.minimumUsernameLength) {
- return next(new Error('[[error:username-too-short]]'));
- }
-
- if (userData.username.length > meta.config.maximumUsernameLength) {
- return next(new Error('[[error:username-too-long]]'));
- }
-
- if (userData.password !== userData['password-confirm']) {
- return next(new Error('[[user:change_password_error_match]]'));
- }
-
- user.isPasswordValid(userData.password, next);
- },
- function (next) {
- res.locals.processLogin = true; // set it to false in plugin if you wish to just register only
- plugins.fireHook('filter:register.check', { req: req, res: res, userData: userData }, next);
- },
- function (result, next) {
- registerAndLoginUser(req, res, userData, next);
- },
- ], function (err, data) {
- if (err) {
- return helpers.noScriptErrors(req, res, err.message, 400);
- }
-
- if (data.uid && req.body.userLang) {
- user.setSetting(data.uid, 'userLang', req.body.userLang);
- }
-
- res.json(data);
- });
-};
+const async = require('async');
+const winston = require('winston');
+const passport = require('passport');
+const nconf = require('nconf');
+const validator = require('validator');
+const _ = require('lodash');
+const util = require('util');
+
+const db = require('../database');
+const meta = require('../meta');
+const user = require('../user');
+const plugins = require('../plugins');
+const utils = require('../utils');
+const translator = require('../translator');
+const helpers = require('./helpers');
+const middleware = require('../middleware');
+const privileges = require('../privileges');
+const sockets = require('../socket.io');
+
+const authenticationController = module.exports;
function registerAndLoginUser(req, res, userData, callback) {
var uid;
@@ -128,6 +73,55 @@ function registerAndLoginUser(req, res, userData, callback) {
], callback);
}
+const registerAndLoginUserAsync = util.promisify(registerAndLoginUser);
+
+
+authenticationController.register = async function (req, res) {
+ const registrationType = meta.config.registrationType || 'normal';
+
+ if (registrationType === 'disabled') {
+ return res.sendStatus(403);
+ }
+
+ var userData = req.body;
+ try {
+ if (registrationType === 'invite-only' || registrationType === 'admin-invite-only') {
+ await user.verifyInvitation(userData);
+ }
+
+ if (!userData.email) {
+ throw new Error('[[error:invalid-email]]');
+ }
+
+ if (!userData.username || userData.username.length < meta.config.minimumUsernameLength || utils.slugify(userData.username).length < meta.config.minimumUsernameLength) {
+ throw new Error('[[error:username-too-short]]');
+ }
+
+ if (userData.username.length > meta.config.maximumUsernameLength) {
+ throw new Error('[[error:username-too-long]]');
+ }
+
+ if (userData.password !== userData['password-confirm']) {
+ throw new Error('[[user:change_password_error_match]]');
+ }
+
+ user.isPasswordValid(userData.password);
+
+ res.locals.processLogin = true; // set it to false in plugin if you wish to just register only
+ await plugins.fireHook('filter:register.check', { req: req, res: res, userData: userData });
+
+ const data = await registerAndLoginUserAsync(req, res, userData);
+
+ if (data.uid && req.body.userLang) {
+ user.setSetting(data.uid, 'userLang', req.body.userLang);
+ }
+
+ res.json(data);
+ } catch (err) {
+ helpers.noScriptErrors(req, res, err.message, 400);
+ }
+};
+
function addToApprovalQueue(req, userData, callback) {
async.waterfall([
function (next) {
diff --git a/src/user/create.js b/src/user/create.js
index 658d909f4e..7312ac1408 100644
--- a/src/user/create.js
+++ b/src/user/create.js
@@ -119,7 +119,7 @@ module.exports = function (User) {
}
if (userData.password) {
- await User.isPasswordValid(userData.password);
+ User.isPasswordValid(userData.password);
}
if (userData.email) {
@@ -130,9 +130,7 @@ module.exports = function (User) {
}
};
- // this function doesnt need to be async, but there is exising code that uses it
- // with a callback so it is marked async otherwise it breaks the callback code
- User.isPasswordValid = async function (password, minStrength) {
+ User.isPasswordValid = function (password, minStrength) {
minStrength = minStrength || meta.config.minimumPasswordStrength;
// Sanity checks: Checks if defined and is string
diff --git a/src/user/password.js b/src/user/password.js
index a57cb0eec9..a4bfc161a5 100644
--- a/src/user/password.js
+++ b/src/user/password.js
@@ -23,7 +23,7 @@ module.exports = function (User) {
hashedPassword = '';
}
- await User.isPasswordValid(password, 0);
+ User.isPasswordValid(password);
await User.auth.logAttempt(uid, ip);
const ok = await Password.compare(password, hashedPassword);
if (ok) {
diff --git a/src/user/profile.js b/src/user/profile.js
index 3c9c13c812..211862703e 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -216,7 +216,7 @@ module.exports = function (User) {
if (uid <= 0 || !data || !data.uid) {
throw new Error('[[error:invalid-uid]]');
}
- await User.isPasswordValid(data.newPassword);
+ User.isPasswordValid(data.newPassword);
const [isAdmin, hasPassword] = await Promise.all([
User.isAdministrator(uid),
User.hasPassword(uid),
diff --git a/src/user/reset.js b/src/user/reset.js
index 7415ae1cb5..841141b5db 100644
--- a/src/user/reset.js
+++ b/src/user/reset.js
@@ -57,7 +57,7 @@ UserReset.send = async function (email) {
};
UserReset.commit = async function (code, password) {
- await user.isPasswordValid(password);
+ user.isPasswordValid(password);
const validated = await UserReset.validate(code);
if (!validated) {
throw new Error('[[error:reset-code-not-valid]]');