Browse Source

fix: Name and username validation should require one letter or number

Owen Diffey 3 years ago
parent
commit
37cfb31da2

+ 2 - 6
backend/logic/actions/users.js

@@ -534,7 +534,6 @@ export default {
 
 
 				// create the user object
 				// create the user object
 				(hash, _id, next) => {
 				(hash, _id, next) => {
-					const avatarColors = ["blue", "orange", "green", "purple", "teal"];
 					next(null, {
 					next(null, {
 						_id,
 						_id,
 						name: username,
 						name: username,
@@ -543,10 +542,6 @@ export default {
 							address: email,
 							address: email,
 							verificationToken
 							verificationToken
 						},
 						},
-						avatar: {
-							type: "initials",
-							color: avatarColors[Math.random(Math.floor(Math.random() * avatarColors.length))]
-						},
 						services: {
 						services: {
 							password: {
 							password: {
 								password: hash
 								password: hash
@@ -558,9 +553,10 @@ export default {
 				// generate the url for gravatar avatar
 				// generate the url for gravatar avatar
 				(user, next) => {
 				(user, next) => {
 					UtilsModule.runJob("CREATE_GRAVATAR", { email: user.email.address }, this).then(url => {
 					UtilsModule.runJob("CREATE_GRAVATAR", { email: user.email.address }, this).then(url => {
+						const avatarColors = ["blue", "orange", "green", "purple", "teal"];
 						user.avatar = {
 						user.avatar = {
 							type: "initials",
 							type: "initials",
-							color: "blue",
+							color: avatarColors[Math.floor(Math.random() * avatarColors.length)],
 							url
 							url
 						};
 						};
 						next(null, user);
 						next(null, user);

+ 11 - 2
backend/logic/db/index.js

@@ -128,7 +128,10 @@ class _DBModule extends CoreClass {
 					this.schemas.user
 					this.schemas.user
 						.path("username")
 						.path("username")
 						.validate(
 						.validate(
-							username => isLength(username, 2, 32) && regex.custom("a-zA-Z0-9_-").test(username),
+							username =>
+								isLength(username, 2, 32) &&
+								regex.custom("a-zA-Z0-9_-").test(username) &&
+								username.replaceAll(/[_]/g, "").length > 0,
 							"Invalid username."
 							"Invalid username."
 						);
 						);
 
 
@@ -140,7 +143,13 @@ class _DBModule extends CoreClass {
 
 
 					this.schemas.user
 					this.schemas.user
 						.path("name")
 						.path("name")
-						.validate(name => isLength(name, 1, 64) && regex.name.test(name), "Invalid name.");
+						.validate(
+							name =>
+								isLength(name, 1, 64) &&
+								regex.name.test(name) &&
+								name.replaceAll(/[ .'_-]/g, "").length > 0,
+							"Invalid name."
+						);
 
 
 					// Station
 					// Station
 					this.schemas.station
 					this.schemas.station

+ 4 - 0
frontend/src/components/modals/Register.vue

@@ -201,6 +201,10 @@ export default {
 				this.username.message =
 				this.username.message =
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 				this.username.valid = false;
 				this.username.valid = false;
+			} else if (value.replaceAll(/[_]/g, "").length === 0) {
+				this.username.message =
+					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
+				this.username.valid = false;
 			} else {
 			} else {
 				this.username.message = "Everything looks great!";
 				this.username.message = "Everything looks great!";
 				this.username.valid = true;
 				this.username.valid = true;

+ 17 - 8
frontend/src/pages/Settings/Tabs/Account.vue

@@ -136,17 +136,21 @@ export default {
 		// prettier-ignore
 		// prettier-ignore
 		// eslint-disable-next-line func-names
 		// eslint-disable-next-line func-names
 		"modifiedUser.username": function (value) {
 		"modifiedUser.username": function (value) {
-		if (!validation.isLength(value, 2, 32)) {
-			this.validation.username.message =
-				"Username must have between 2 and 32 characters.";
-			this.validation.username.valid = false;
-		} else if (
-			!validation.regex.azAZ09_.test(value) &&
-			value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
-		) {
+			if (!validation.isLength(value, 2, 32)) {
+				this.validation.username.message =
+					"Username must have between 2 and 32 characters.";
+				this.validation.username.valid = false;
+			} else if (
+				!validation.regex.azAZ09_.test(value) &&
+				value !== this.originalUser.username // Sometimes a username pulled from GitHub won't succeed validation
+			) {
 				this.validation.username.message =
 				this.validation.username.message =
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _.";
 				this.validation.username.valid = false;
 				this.validation.username.valid = false;
+			} else if (value.replaceAll(/[_]/g, "").length === 0) {
+				this.validation.username.message =
+					"Invalid format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number.";
+				this.validation.username.valid = false;
 			} else {
 			} else {
 				this.validation.username.message = "Everything looks great!";
 				this.validation.username.message = "Everything looks great!";
 				this.validation.username.valid = true;
 				this.validation.username.valid = true;
@@ -256,6 +260,11 @@ export default {
 					"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
 					"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _."
 				);
 				);
 
 
+			if (username.replaceAll(/[_]/g, "").length === 0)
+				return new Toast(
+					"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
+				);
+
 			this.$refs.saveButton.saveStatus = "disabled";
 			this.$refs.saveButton.saveStatus = "disabled";
 
 
 			return this.socket.dispatch(
 			return this.socket.dispatch(

+ 2 - 2
frontend/src/pages/Settings/Tabs/Profile.vue

@@ -150,9 +150,9 @@ export default {
 				return new Toast(
 				return new Toast(
 					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed."
 					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed."
 				);
 				);
-			if (name.replaceAll(/[0-9 .'_-]/g, "").length === 0)
+			if (name.replaceAll(/[ .'_-]/g, "").length === 0)
 				return new Toast(
 				return new Toast(
-					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed, and there has to be at least one letter."
+					"Invalid name format. Only letters, numbers, spaces, apostrophes, underscores and hyphens are allowed, and there has to be at least one letter or number."
 				);
 				);
 
 
 			this.$refs.saveButton.status = "disabled";
 			this.$refs.saveButton.status = "disabled";

+ 7 - 0
frontend/src/store/modules/user.js

@@ -62,6 +62,13 @@ const modules = {
 							)
 							)
 						);
 						);
 
 
+					if (username.replaceAll(/[_]/g, "").length === 0)
+						return reject(
+							new Error(
+								"Invalid username format. Allowed characters: a-z, A-Z, 0-9 and _, and there has to be at least one letter or number."
+							)
+						);
+
 					if (!validation.isLength(password, 6, 200))
 					if (!validation.isLength(password, 6, 200))
 						return reject(
 						return reject(
 							new Error(
 							new Error(