|
|
@ -1,7 +1,11 @@
|
|
|
|
let regiterFields = document.getElementById("field-username");
|
|
|
|
let registerField = document.getElementById("field-username");
|
|
|
|
|
|
|
|
let connectUsernameField = document.getElementById("connect-field-username");
|
|
|
|
|
|
|
|
let connectPasswordField = document.getElementById("connect-field-password");
|
|
|
|
let inputFields = document.getElementsByClassName("field");
|
|
|
|
let inputFields = document.getElementsByClassName("field");
|
|
|
|
let registerForm = document.getElementById("form");
|
|
|
|
let registerForm = document.getElementById("register-form");
|
|
|
|
let submitButton = document.getElementById("button-submit");
|
|
|
|
let registerButton = document.getElementById("button-register-submit");
|
|
|
|
|
|
|
|
let connectForm = document.getElementById("connect-form");
|
|
|
|
|
|
|
|
let connectButton = document.getElementById("button-connect-submit");
|
|
|
|
let message = document.getElementById("message");
|
|
|
|
let message = document.getElementById("message");
|
|
|
|
let tabLinkButtons = document.getElementsByClassName("tablinks");
|
|
|
|
let tabLinkButtons = document.getElementsByClassName("tablinks");
|
|
|
|
let tabContentBlocks = document.getElementsByClassName("tabcontent");
|
|
|
|
let tabContentBlocks = document.getElementsByClassName("tabcontent");
|
|
|
@ -29,7 +33,7 @@ let hideMessage = function() {
|
|
|
|
message.classList.add("hidden");
|
|
|
|
message.classList.add("hidden");
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let onResponse = function(response, success) {
|
|
|
|
let onRegisterResponse = function(response, success) {
|
|
|
|
// Display message
|
|
|
|
// Display message
|
|
|
|
showMessage(response);
|
|
|
|
showMessage(response);
|
|
|
|
|
|
|
|
|
|
|
@ -39,10 +43,26 @@ let onResponse = function(response, success) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Enable submit button and input field
|
|
|
|
// Enable submit button and input field
|
|
|
|
submitButton.classList.remove('button--disabled');
|
|
|
|
registerButton.classList.remove('button--disabled');
|
|
|
|
submitButton.value = "Submit"
|
|
|
|
registerButton.value = "Submit"
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let onConnectResponse = function(response, success) {
|
|
|
|
|
|
|
|
// Display message
|
|
|
|
|
|
|
|
showMessage(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(success) {
|
|
|
|
|
|
|
|
connectForm.submit();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable submit button and input field
|
|
|
|
|
|
|
|
connectButton.classList.remove('button--disabled');
|
|
|
|
|
|
|
|
connectButton.value = "Submit"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let allowedUsernameCharacters = RegExp("[^a-z0-9\\.\\_\\=\\-\\/]");
|
|
|
|
let allowedUsernameCharacters = RegExp("[^a-z0-9\\.\\_\\=\\-\\/]");
|
|
|
|
let usernameIsValid = function(username) {
|
|
|
|
let usernameIsValid = function(username) {
|
|
|
|
return !allowedUsernameCharacters.test(username);
|
|
|
|
return !allowedUsernameCharacters.test(username);
|
|
|
@ -62,13 +82,13 @@ let buildQueryString = function(params) {
|
|
|
|
.join('&');
|
|
|
|
.join('&');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let submitUsername = function(username) {
|
|
|
|
let submitRegister = function(username) {
|
|
|
|
if(username.length == 0) {
|
|
|
|
if(username.length == 0) {
|
|
|
|
onResponse("Please enter a username.", false);
|
|
|
|
onRegisterResponse("Please enter a username.", false);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!usernameIsValid(username)) {
|
|
|
|
if(!usernameIsValid(username)) {
|
|
|
|
onResponse("Invalid username. Only the following characters are allowed: " + allowedCharactersString, false);
|
|
|
|
onRegisterResponse("Invalid username. Only the following characters are allowed: " + allowedCharactersString, false);
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -86,27 +106,92 @@ let submitUsername = function(username) {
|
|
|
|
if(json.error) {
|
|
|
|
if(json.error) {
|
|
|
|
throw json.error;
|
|
|
|
throw json.error;
|
|
|
|
} else if(json.available) {
|
|
|
|
} else if(json.available) {
|
|
|
|
onResponse("Success. Please wait a moment for your browser to redirect.", true);
|
|
|
|
onRegisterResponse("Success. Please wait a moment for your browser to redirect.", true);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
onResponse("This username is not available, please choose another.", false);
|
|
|
|
onRegisterResponse("This username is not available, please choose another.", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
}).catch((err) => {
|
|
|
|
onResponse("Error checking username availability: " + err, false);
|
|
|
|
onRegisterResponse("Error checking username availability: " + err, false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let clickSubmit = function() {
|
|
|
|
let submitConnect = function(username, password) {
|
|
|
|
if(submitButton.classList.contains('button--disabled')) { return; }
|
|
|
|
if(username.length == 0) {
|
|
|
|
|
|
|
|
onConnectResponse("Please enter a username.", false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!usernameIsValid(username)) {
|
|
|
|
|
|
|
|
onConnectResponse("Invalid username. Only the following characters are allowed: " + allowedCharactersString, false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(password.length == 0) {
|
|
|
|
|
|
|
|
onConnectResponse("Please enter a password.", false);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
details = {
|
|
|
|
|
|
|
|
username: username,
|
|
|
|
|
|
|
|
password: password
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var formBody = [];
|
|
|
|
|
|
|
|
for (var property in details) {
|
|
|
|
|
|
|
|
var encodedKey = encodeURIComponent(property);
|
|
|
|
|
|
|
|
var encodedValue = encodeURIComponent(details[property]);
|
|
|
|
|
|
|
|
formBody.push(encodedKey + "=" + encodedValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
formBody = formBody.join("&");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fetch('check', {
|
|
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
body: formBody
|
|
|
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
|
|
|
if(!response.ok) {
|
|
|
|
|
|
|
|
// for non-200 responses, raise the body of the response as an exception
|
|
|
|
|
|
|
|
return response.text().then((text) => { throw text });
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).then((json) => {
|
|
|
|
|
|
|
|
if(json.error) {
|
|
|
|
|
|
|
|
throw json.error;
|
|
|
|
|
|
|
|
} else if(json.success) {
|
|
|
|
|
|
|
|
onConnectResponse("Success. Please wait a moment for your browser to redirect.", true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
onConnectResponse("The credentials you entered are not valid.", false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}).catch((err) => {
|
|
|
|
|
|
|
|
onConnectResponse("Error checking credentials: " + err, false);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let registerClickSubmit = function() {
|
|
|
|
|
|
|
|
if(registerButton.classList.contains('button--disabled')) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
// Disable submit button and input field
|
|
|
|
// Disable submit button and input field
|
|
|
|
submitButton.classList.add('button--disabled');
|
|
|
|
registerButton.classList.add('button--disabled');
|
|
|
|
|
|
|
|
|
|
|
|
// Submit username
|
|
|
|
// Submit username
|
|
|
|
submitButton.value = "Checking...";
|
|
|
|
registerButton.value = "Checking...";
|
|
|
|
submitUsername(regiterFields.value);
|
|
|
|
submitRegister(registerField.value);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
submitButton.onclick = clickSubmit;
|
|
|
|
let connectClickSubmit = function() {
|
|
|
|
|
|
|
|
if(connectButton.classList.contains('button--disabled')) { return; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Disable submit button and input field
|
|
|
|
|
|
|
|
connectButton.classList.add('button--disabled');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Submit data
|
|
|
|
|
|
|
|
connectButton.value = "Checking...";
|
|
|
|
|
|
|
|
submitConnect(connectUsernameField.value, connectPasswordField.value);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
registerButton.onclick = registerClickSubmit;
|
|
|
|
|
|
|
|
connectButton.onclick = connectClickSubmit;
|
|
|
|
|
|
|
|
|
|
|
|
// Listen for events on inputFields
|
|
|
|
// Listen for events on inputFields
|
|
|
|
for (let i = 0; i < inputFields.length; i++) {
|
|
|
|
for (let i = 0; i < inputFields.length; i++) {
|
|
|
@ -114,7 +199,11 @@ for (let i = 0; i < inputFields.length; i++) {
|
|
|
|
// Listen for Enter on input field
|
|
|
|
// Listen for Enter on input field
|
|
|
|
if (event.which === 13) {
|
|
|
|
if (event.which === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.preventDefault();
|
|
|
|
clickSubmit();
|
|
|
|
if (inputFields[i].id === "field-username") {
|
|
|
|
|
|
|
|
registerClickSubmit();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
connectClickSubmit();
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switchClass(inputFields[i]);
|
|
|
|
switchClass(inputFields[i]);
|
|
|
|