From bfd512b99b31de0911cc3e077071002296b22144 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 5 Mar 2021 11:20:14 -0500 Subject: [PATCH] feat: expose username validation logic to user lib, new hook `filter:username.check` --- src/user/profile.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) 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) {