Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the username issue, giving users a suggestion for a new username. #12

Open
wants to merge 2 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added dump.rdb
Binary file not shown.
3 changes: 2 additions & 1 deletion public/src/client/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@
if (results.every(obj => obj.status === 'rejected')) {
showSuccess(usernameInput, username_notify, successIcon);
} else {
showError(usernameInput, username_notify, '[[error:username-taken]]');
showError(usernameInput, username_notify, '[[error:username-taken]]' + '. Maybe try "' + $('#username').val() + '123suffix' + '"');

Check failure on line 138 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Unexpected string concatenation of literals

Check failure on line 138 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Unexpected string concatenation of literals

Check failure on line 138 in public/src/client/register.js

View workflow job for this annotation

GitHub Actions / test

Block must not be padded by blank lines

}

callback();
Expand Down
33 changes: 32 additions & 1 deletion src/user/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,38 @@
}
const exists = await User.existsBySlug(userslug);
if (exists) {
throw new Error('[[error:username-taken]]');
async function suggestAlternativeUsername(username) {

Check failure on line 130 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

Move function declaration to function body root
return `${username}-suffix`;
}

Check failure on line 133 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
async function registerUsername(username) {

Check failure on line 134 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

Move function declaration to function body root
try {
let isUsernameTaken = await checkUsernameAvailability(username);

Check failure on line 136 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

'isUsernameTaken' is never reassigned. Use 'const' instead

Check failure on line 137 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
if (isUsernameTaken) {
throw new Error('[[error:username-taken]]');
}

Check failure on line 141 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

Trailing spaces not allowed
console.log('Username is available.');
} catch (error) {
if (error.message === '[[error:username-taken]]') {
let alternativeUsername = await suggestAlternativeUsername(username);

Check failure on line 145 in src/user/profile.js

View workflow job for this annotation

GitHub Actions / test

'alternativeUsername' is never reassigned. Use 'const' instead
console.log(`Username is taken. How about: ${alternativeUsername}`);
} else {
console.error(error);
}
}
}

async function checkUsernameAvailability(username) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, 1000);
});
}

registerUsername('desiredUsername');
}

const { error } = await plugins.hooks.fire('filter:username.check', {
Expand Down