Verify username as valid if user is re-checking their current name (#10737)

* Verify username as valid if user is re-checking their current name

* Fix lint error and existingUser check.
This commit is contained in:
Phillip Thelen
2018-10-05 21:51:21 +02:00
committed by Sabe Jones
parent 8682cf1cf7
commit 52fbb8f899

View File

@@ -73,6 +73,8 @@ api.verifyUsername = {
method: 'POST',
url: '/user/auth/verify-username',
async handler (req, res) {
const user = res.locals.user;
req.checkBody({
username: {
notEmpty: {errorMessage: res.t('missingUsername')},
@@ -84,8 +86,10 @@ api.verifyUsername = {
const issues = verifyUsername(req.body.username, res);
const count = await User.count({ 'auth.local.lowerCaseUsername': req.body.username.toLowerCase() });
if (count > 0) issues.push(res.t('usernameTaken'));
const existingUser = await User.findOne({ 'auth.local.lowerCaseUsername': req.body.username.toLowerCase() }, {auth: 1}).exec();
if (existingUser && existingUser._id !== user._id) {
issues.push(res.t('usernameTaken'));
}
if (issues.length > 0) {
res.respond(200, { isUsable: false, issues });