Skip to content
Closed
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
1 change: 1 addition & 0 deletions public/language/en-US/error.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"folder-exists": "Folder exists",
"invalid-pagination-value": "Invalid pagination value, must be at least %1 and at most %2",
"username-taken": "Username taken",
"username-suggestion": "Try: %1",
"email-taken": "Email address is already taken.",
"email-nochange": "The email entered is the same as the email already on file.",
"email-invited": "Email was already invited",
Expand Down
154 changes: 103 additions & 51 deletions public/src/client/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,73 +114,125 @@
$('#username').trigger('focus');
};

function validateUsername(username, callback) {
callback = callback || function () {};

const username_notify = $('#username-notify');
username_notify.text('');
const usernameInput = $('#username');
const userslug = slugify(username);
if (username.length < ajaxify.data.minimumUsernameLength || userslug.length < ajaxify.data.minimumUsernameLength) {
showError(usernameInput, username_notify, '[[error:username-too-short]]');
} else if (username.length > ajaxify.data.maximumUsernameLength) {
showError(usernameInput, username_notify, '[[error:username-too-long]]');
} else if (!utils.isUserNameValid(username) || !userslug) {
showError(usernameInput, username_notify, '[[error:invalid-username]]');
} else {
function validateUsername(username, callback) {

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 2
callback = callback || function () {};

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3

const username_notify = $('#username-notify');

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3
const usernameInput = $('#username');

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3
username_notify.empty();

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3

const userslug = slugify(username);

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3

if (

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3
username.length < ajaxify.data.minimumUsernameLength ||

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 4
userslug.length < ajaxify.data.minimumUsernameLength
) {

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 3
showError(usernameInput, username_notify, '[[error:username-too-short]]');

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

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 4
callback();
return;
}

if (username.length > ajaxify.data.maximumUsernameLength) {
showError(usernameInput, username_notify, '[[error:username-too-long]]');
callback();
return;
}

if (!utils.isUserNameValid(username) || !userslug) {
showError(usernameInput, username_notify, '[[error:invalid-username]]');
callback();
return;
}

Promise.allSettled([
api.head(`/users/bySlug/${userslug}`, {}),
api.head(`/groups/${username}`, {}),
]).then((results) => {
if (results.every(obj => obj.status === 'rejected')) {
if (results.every(r => r.status === 'rejected')) {
showSuccess(usernameInput, username_notify, successIcon);
} else {
showError(usernameInput, username_notify, '[[error:username-taken]]');
callback();
return;
}

callback();
// ---------- username taken ----------
validationError = true;
usernameInput.attr('aria-invalid', 'true');

const maxLen = ajaxify.data.maximumUsernameLength || 16;
let suggested = `${userslug}1`;
if (suggested.length > maxLen) {
suggested = suggested.slice(0, maxLen);
}

translator.translate('[[error:username-taken]]', function (takenMsg) {
translator.translate(
`[[error:username-suggestion, ${suggested}]]`,
function (suggestMsg) {
username_notify.html(
`${takenMsg} &nbsp; <a href="#" class="username-suggestion">${suggestMsg}</a>`
);

username_notify.parent()
.removeClass('register-success')
.addClass('register-danger');

username_notify.show();

username_notify
.off('click.usernameSuggestion')
.on('click.usernameSuggestion', 'a.username-suggestion', function (e) {
e.preventDefault();
usernameInput.val(suggested);
$('#yourUsername').text(slugify(suggested));
validateUsername(suggested);
});

callback();
}
);
});
});
}
}

function validatePassword(password, password_confirm) {
const passwordInput = $('#password');
const password_notify = $('#password-notify');
const password_confirm_notify = $('#password-confirm-notify');
password_notify.text('');
password_confirm_notify.text('');
try {
utils.assertPasswordValidity(password, zxcvbn);

if (password === $('#username').val()) {
throw new Error('[[user:password-same-as-username]]');
}

showSuccess(passwordInput, password_notify, successIcon);
} catch (err) {
showError(passwordInput, password_notify, err.message);
}
function validatePassword(password, password_confirm) {
const passwordInput = $('#password');
const password_notify = $('#password-notify');
const password_confirm_notify = $('#password-confirm-notify');
password_notify.text('');
password_confirm_notify.text('');
try {
utils.assertPasswordValidity(password, zxcvbn);

if (password !== password_confirm && password_confirm !== '') {
showError(passwordInput, password_confirm_notify, '[[user:change-password-error-match]]');
}
}
if (password === $('#username').val()) {
throw new Error('[[user:password-same-as-username]]');
}

showSuccess(passwordInput, password_notify, successIcon);
} catch (err) {
showError(passwordInput, password_notify, err.message);
}

function validatePasswordConfirm(password, password_confirm) {
const passwordConfirmInput = $('#password-confirm');
const password_notify = $('#password-notify');
const password_confirm_notify = $('#password-confirm-notify');
password_confirm_notify.text('');
if (!password || password_notify.hasClass('alert-error')) {
return;
if (password !== password_confirm && password_confirm !== '') {
showError(passwordInput, password_confirm_notify, '[[user:change-password-error-match]]');
}
}

if (password !== password_confirm) {
showError(passwordConfirmInput, password_confirm_notify, '[[user:change-password-error-match]]');
} else {
showSuccess(passwordConfirmInput, password_confirm_notify, successIcon);
function validatePasswordConfirm(password, password_confirm) {
const passwordConfirmInput = $('#password-confirm');
const password_notify = $('#password-notify');
const password_confirm_notify = $('#password-confirm-notify');
password_confirm_notify.text('');
if (!password || password_notify.hasClass('alert-error')) {
return;
}

if (password !== password_confirm) {
showError(passwordConfirmInput, password_confirm_notify, '[[user:change-password-error-match]]');
} else {
showSuccess(passwordConfirmInput, password_confirm_notify, successIcon);
}
}
}

function showError(input, element, msg) {
translator.translate(msg, function (msg) {
Expand Down