2021-02-01 08:52:50 -07:00
|
|
|
const usernameField = document.getElementById("field-username");
|
2021-02-03 10:52:55 -07:00
|
|
|
const usernameOutput = document.getElementById("field-username-output");
|
|
|
|
const form = document.getElementById("form");
|
|
|
|
|
|
|
|
// needed to validate on change event when no input was changed
|
|
|
|
let needsValidation = true;
|
|
|
|
let isValid = false;
|
2021-02-01 08:52:50 -07:00
|
|
|
|
|
|
|
function throttle(fn, wait) {
|
|
|
|
let timeout;
|
2021-02-03 10:52:55 -07:00
|
|
|
const throttleFn = function() {
|
2021-02-01 08:52:50 -07:00
|
|
|
const args = Array.from(arguments);
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
timeout = setTimeout(fn.bind.apply(fn, [null].concat(args)), wait);
|
2021-02-03 10:52:55 -07:00
|
|
|
};
|
|
|
|
throttleFn.cancelQueued = function() {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
return throttleFn;
|
2021-02-01 08:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkUsernameAvailable(username) {
|
|
|
|
let check_uri = 'check?username=' + encodeURIComponent(username);
|
|
|
|
return fetch(check_uri, {
|
|
|
|
// include the cookie
|
|
|
|
"credentials": "same-origin",
|
2021-02-03 10:52:55 -07:00
|
|
|
}).then(function(response) {
|
2021-02-01 08:52:50 -07:00
|
|
|
if(!response.ok) {
|
|
|
|
// for non-200 responses, raise the body of the response as an exception
|
|
|
|
return response.text().then((text) => { throw new Error(text); });
|
|
|
|
} else {
|
|
|
|
return response.json();
|
|
|
|
}
|
2021-02-03 10:52:55 -07:00
|
|
|
}).then(function(json) {
|
2021-02-01 08:52:50 -07:00
|
|
|
if(json.error) {
|
|
|
|
return {message: json.error};
|
|
|
|
} else if(json.available) {
|
|
|
|
return {available: true};
|
|
|
|
} else {
|
|
|
|
return {message: username + " is not available, please choose another."};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-03 10:52:55 -07:00
|
|
|
const allowedUsernameCharacters = new RegExp("^[a-z0-9\\.\\_\\-\\/\\=]+$");
|
|
|
|
const allowedCharactersString = "lowercase letters, digits, ., _, -, /, =";
|
|
|
|
|
|
|
|
function reportError(error) {
|
|
|
|
throttledCheckUsernameAvailable.cancelQueued();
|
|
|
|
usernameOutput.innerText = error;
|
|
|
|
usernameOutput.classList.add("error");
|
|
|
|
usernameField.parentElement.classList.add("invalid");
|
|
|
|
usernameField.focus();
|
|
|
|
}
|
|
|
|
|
2021-02-01 08:52:50 -07:00
|
|
|
function validateUsername(username) {
|
2021-02-03 10:52:55 -07:00
|
|
|
isValid = false;
|
|
|
|
needsValidation = false;
|
|
|
|
usernameOutput.innerText = "";
|
|
|
|
usernameField.parentElement.classList.remove("invalid");
|
|
|
|
usernameOutput.classList.remove("error");
|
|
|
|
if (!username) {
|
2022-03-22 04:22:25 -06:00
|
|
|
return reportError("This is required. Please provide a username");
|
2021-02-01 08:52:50 -07:00
|
|
|
}
|
2021-02-03 10:52:55 -07:00
|
|
|
if (username.length > 255) {
|
|
|
|
return reportError("Too long, please choose something shorter");
|
2021-02-01 08:52:50 -07:00
|
|
|
}
|
2021-02-03 10:52:55 -07:00
|
|
|
if (!allowedUsernameCharacters.test(username)) {
|
|
|
|
return reportError("Invalid username, please only use " + allowedCharactersString);
|
|
|
|
}
|
|
|
|
usernameOutput.innerText = "Checking if username is available …";
|
2021-02-01 08:52:50 -07:00
|
|
|
throttledCheckUsernameAvailable(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
const throttledCheckUsernameAvailable = throttle(function(username) {
|
2021-02-03 10:52:55 -07:00
|
|
|
const handleError = function(err) {
|
2021-02-01 08:52:50 -07:00
|
|
|
// don't prevent form submission on error
|
2021-02-03 10:52:55 -07:00
|
|
|
usernameOutput.innerText = "";
|
|
|
|
isValid = true;
|
2021-02-01 08:52:50 -07:00
|
|
|
};
|
|
|
|
try {
|
|
|
|
checkUsernameAvailable(username).then(function(result) {
|
|
|
|
if (!result.available) {
|
2021-02-03 10:52:55 -07:00
|
|
|
reportError(result.message);
|
2021-02-01 08:52:50 -07:00
|
|
|
} else {
|
2021-02-03 10:52:55 -07:00
|
|
|
isValid = true;
|
|
|
|
usernameOutput.innerText = "";
|
2021-02-01 08:52:50 -07:00
|
|
|
}
|
|
|
|
}, handleError);
|
|
|
|
} catch (err) {
|
|
|
|
handleError(err);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
|
2021-02-03 10:52:55 -07:00
|
|
|
form.addEventListener("submit", function(evt) {
|
|
|
|
if (needsValidation) {
|
|
|
|
validateUsername(usernameField.value);
|
|
|
|
evt.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!isValid) {
|
|
|
|
evt.preventDefault();
|
|
|
|
usernameField.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2021-02-01 08:52:50 -07:00
|
|
|
usernameField.addEventListener("input", function(evt) {
|
|
|
|
validateUsername(usernameField.value);
|
|
|
|
});
|
|
|
|
usernameField.addEventListener("change", function(evt) {
|
2021-02-03 10:52:55 -07:00
|
|
|
if (needsValidation) {
|
|
|
|
validateUsername(usernameField.value);
|
|
|
|
}
|
2021-02-01 08:52:50 -07:00
|
|
|
});
|