mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: remove async from isPasswordValid, function is sync
This commit is contained in:
@@ -1,80 +1,25 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const async = require('async');
|
||||||
var winston = require('winston');
|
const winston = require('winston');
|
||||||
var passport = require('passport');
|
const passport = require('passport');
|
||||||
var nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
var validator = require('validator');
|
const validator = require('validator');
|
||||||
var _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
var db = require('../database');
|
const db = require('../database');
|
||||||
var meta = require('../meta');
|
const meta = require('../meta');
|
||||||
var user = require('../user');
|
const user = require('../user');
|
||||||
var plugins = require('../plugins');
|
const plugins = require('../plugins');
|
||||||
var utils = require('../utils');
|
const utils = require('../utils');
|
||||||
var translator = require('../translator');
|
const translator = require('../translator');
|
||||||
var helpers = require('./helpers');
|
const helpers = require('./helpers');
|
||||||
var middleware = require('../middleware');
|
const middleware = require('../middleware');
|
||||||
var privileges = require('../privileges');
|
const privileges = require('../privileges');
|
||||||
var sockets = require('../socket.io');
|
const sockets = require('../socket.io');
|
||||||
|
|
||||||
var authenticationController = module.exports;
|
const 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);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function registerAndLoginUser(req, res, userData, callback) {
|
function registerAndLoginUser(req, res, userData, callback) {
|
||||||
var uid;
|
var uid;
|
||||||
@@ -128,6 +73,55 @@ function registerAndLoginUser(req, res, userData, callback) {
|
|||||||
], 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) {
|
function addToApprovalQueue(req, userData, callback) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ module.exports = function (User) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (userData.password) {
|
if (userData.password) {
|
||||||
await User.isPasswordValid(userData.password);
|
User.isPasswordValid(userData.password);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userData.email) {
|
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
|
User.isPasswordValid = function (password, minStrength) {
|
||||||
// with a callback so it is marked async otherwise it breaks the callback code
|
|
||||||
User.isPasswordValid = async function (password, minStrength) {
|
|
||||||
minStrength = minStrength || meta.config.minimumPasswordStrength;
|
minStrength = minStrength || meta.config.minimumPasswordStrength;
|
||||||
|
|
||||||
// Sanity checks: Checks if defined and is string
|
// Sanity checks: Checks if defined and is string
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ module.exports = function (User) {
|
|||||||
hashedPassword = '';
|
hashedPassword = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
await User.isPasswordValid(password, 0);
|
User.isPasswordValid(password);
|
||||||
await User.auth.logAttempt(uid, ip);
|
await User.auth.logAttempt(uid, ip);
|
||||||
const ok = await Password.compare(password, hashedPassword);
|
const ok = await Password.compare(password, hashedPassword);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ module.exports = function (User) {
|
|||||||
if (uid <= 0 || !data || !data.uid) {
|
if (uid <= 0 || !data || !data.uid) {
|
||||||
throw new Error('[[error:invalid-uid]]');
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
await User.isPasswordValid(data.newPassword);
|
User.isPasswordValid(data.newPassword);
|
||||||
const [isAdmin, hasPassword] = await Promise.all([
|
const [isAdmin, hasPassword] = await Promise.all([
|
||||||
User.isAdministrator(uid),
|
User.isAdministrator(uid),
|
||||||
User.hasPassword(uid),
|
User.hasPassword(uid),
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ UserReset.send = async function (email) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
UserReset.commit = async function (code, password) {
|
UserReset.commit = async function (code, password) {
|
||||||
await user.isPasswordValid(password);
|
user.isPasswordValid(password);
|
||||||
const validated = await UserReset.validate(code);
|
const validated = await UserReset.validate(code);
|
||||||
if (!validated) {
|
if (!validated) {
|
||||||
throw new Error('[[error:reset-code-not-valid]]');
|
throw new Error('[[error:reset-code-not-valid]]');
|
||||||
|
|||||||
Reference in New Issue
Block a user