Skip to content

Node.js based forum software built for the modern web

refactor: checkUsername function https://github.com/NodeBB/NodeBB/issues/10864

Barış Soner Uşaklı7e27da61ab2c
diff --git a/src/controllers/authentication.js b/src/controllers/authentication.js
index b7873b6a28..f948e053b5 100644
--- a/src/controllers/authentication.js
+++ b/src/controllers/authentication.js
@@ -84,17 +84,7 @@ authenticationController.register = async function (req, res) {
await user.verifyInvitation(userData);
}
- if (
- !userData.username ||
- userData.username.length < meta.config.minimumUsernameLength ||
- 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]]');
- }
+ user.checkUsernameLength(userData.username);
if (userData.password !== userData['password-confirm']) {
throw new Error('[[user:change-password-error-match]]');
diff --git a/src/user/profile.js b/src/user/profile.js
index ec4a23d3a3..b5e878eafa 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -170,13 +170,7 @@ module.exports = function (User) {
}
}
- if (data.username.length < meta.config.minimumUsernameLength) {
- throw new Error('[[error:username-too-short]]');
- }
-
- if (data.username.length > meta.config.maximumUsernameLength) {
- throw new Error('[[error:username-too-long]]');
- }
+ User.checkUsernameLength(data.username);
const userslug = slugify(data.username);
if (!utils.isUserNameValid(data.username) || !userslug) {
@@ -201,6 +195,20 @@ module.exports = function (User) {
}
User.checkUsername = async username => isUsernameAvailable({ username });
+ User.checkUsernameLength = function (username) {
+ if (
+ !username ||
+ username.length < meta.config.minimumUsernameLength ||
+ slugify(username).length < meta.config.minimumUsernameLength
+ ) {
+ throw new Error('[[error:username-too-short]]');
+ }
+
+ if (username.length > meta.config.maximumUsernameLength) {
+ throw new Error('[[error:username-too-long]]');
+ }
+ };
+
async function isAboutMeValid(callerUid, data) {
if (!data.aboutme) {
return;