Skip to content

Node.js based forum software built for the modern web

feat: expose username validation logic to user lib, new hook `filter:username.check`

Julian Lambfd512b99b31
diff --git a/src/user/profile.js b/src/user/profile.js
index ba68b7ae0c..bc9ec7a6b3 100644
--- a/src/user/profile.js
+++ b/src/user/profile.js
@@ -106,9 +106,13 @@ module.exports = function (User) {
return;
}
data.username = data.username.trim();
- const userData = await User.getUserFields(uid, ['username', 'userslug']);
- if (userData.username === data.username) {
- return;
+
+ let userData;
+ if (uid) {
+ userData = await User.getUserFields(uid, ['username', 'userslug']);
+ if (userData.username === data.username) {
+ return;
+ }
}
if (data.username.length < meta.config.minimumUsernameLength) {
@@ -124,14 +128,23 @@ module.exports = function (User) {
throw new Error('[[error:invalid-username]]');
}
- if (userslug === userData.userslug) {
+ if (uid && userslug === userData.userslug) {
return;
}
const exists = await User.existsBySlug(userslug);
if (exists) {
throw new Error('[[error:username-taken]]');
}
+
+ const { error } = await plugins.hooks.fire('filter:username.check', {
+ username: data.username,
+ error: undefined,
+ });
+ if (error) {
+ throw error;
+ }
}
+ User.checkUsername = async username => isUsernameAvailable({ username });
async function isWebsiteValid(callerUid, data) {
if (!data.website) {