2023-11-29 23:20:25 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
|
|
const signupForm = document.forms.signupForm;
|
|
|
|
const loginForm = document.forms.loginForm;
|
|
|
|
|
|
|
|
const submitBtn = document.getElementById("submitBtn");
|
|
|
|
|
|
|
|
signupForm.addEventListener("input", function () {
|
|
|
|
const formIsValid = signupForm.checkValidity();
|
|
|
|
submitBtn.disabled = !formIsValid;
|
|
|
|
});
|
|
|
|
|
2023-11-29 23:58:43 +00:00
|
|
|
signupForm.addEventListener("submit", function (event) {
|
|
|
|
event.preventDefault(); // Prevent the form from being submitted normally
|
|
|
|
submitSignup();
|
|
|
|
});
|
|
|
|
|
2023-11-30 05:30:12 +00:00
|
|
|
const usernameInput = document.getElementById("signup_username");
|
|
|
|
usernameInput.addEventListener("blur", function () {
|
|
|
|
checkUsernameAvailability();
|
|
|
|
});
|
|
|
|
|
2023-11-29 23:58:43 +00:00
|
|
|
|
2023-11-29 23:20:25 +00:00
|
|
|
loginForm.addEventListener("submit", function (event) {
|
|
|
|
event.preventDefault(); // Prevent the form from being submitted normally
|
|
|
|
submitLogin();
|
|
|
|
});
|
|
|
|
|
|
|
|
var urlParams = new URLSearchParams(window.location.search);
|
|
|
|
if (urlParams.has('login')) {
|
|
|
|
console.log("login parameter")
|
|
|
|
$('#loginModal').modal('show');
|
|
|
|
}
|
|
|
|
else if (urlParams.has('signup')) {
|
|
|
|
$('#signupModal').modal('show');
|
|
|
|
console.log("signup parameter")
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function checkUsernameAvailability() {
|
2023-11-30 05:30:12 +00:00
|
|
|
const usernameInput = document.getElementById("signup_username");
|
2023-11-29 23:20:25 +00:00
|
|
|
const availabilityDiv = document.getElementById("usernameAvailability");
|
|
|
|
|
|
|
|
// Get the value of the username input
|
|
|
|
const username = usernameInput.value;
|
|
|
|
|
|
|
|
// Check if the input value meets the required pattern
|
|
|
|
const usernameRegex = new RegExp(usernameInput.pattern);
|
|
|
|
if (!usernameRegex.test(username)) {
|
|
|
|
availabilityDiv.innerHTML = "";
|
|
|
|
return; // Exit the function if the username is not valid
|
|
|
|
}
|
|
|
|
// Send an AJAX request to the server
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", "/checkusername?username=" + username, true);
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4 && xhr.status === 200) {
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
const isAvailable = response.available;
|
|
|
|
if (!isAvailable) {
|
|
|
|
availabilityDiv.innerHTML = "<span style='color:green;'>Username is available.</span>";
|
|
|
|
} else {
|
|
|
|
availabilityDiv.innerHTML = "<span style='color:red;'>Username is not available.</span>";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send();
|
2023-11-29 23:58:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function submitSignup() {
|
|
|
|
// Disable the button to prevent multiple clicks
|
|
|
|
submitBtn.disabled = true;
|
|
|
|
|
|
|
|
// Show the spinner
|
|
|
|
const spinner = document.createElement('i');
|
|
|
|
spinner.classList.add('fas', 'fa-spinner', 'fa-spin');
|
|
|
|
submitBtn.innerHTML = '';
|
|
|
|
submitBtn.appendChild(spinner);
|
|
|
|
|
2023-11-30 05:30:12 +00:00
|
|
|
// Get the values of the form fields
|
|
|
|
const signup_username = document.getElementById("signup_username").value;
|
|
|
|
const signup_password = document.getElementById("signup_password").value;
|
2023-11-29 23:58:43 +00:00
|
|
|
const name = document.getElementById("name").value;
|
|
|
|
const email = document.getElementById("email").value;
|
|
|
|
|
|
|
|
// Create a new FormData object and append the values
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("name", name);
|
|
|
|
formData.append("email", email);
|
2023-11-30 05:30:12 +00:00
|
|
|
formData.append("username", signup_username);
|
|
|
|
formData.append("password", signup_password);
|
2023-11-29 23:58:43 +00:00
|
|
|
|
|
|
|
// Send the signup request as form data
|
2023-11-30 03:03:28 +00:00
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", "/signup", true);
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
// Signup successful
|
|
|
|
console.log("Signup successful");
|
|
|
|
window.location.href = "/welcome"; // Redirect to success page
|
|
|
|
} else {
|
|
|
|
// Signup failed, display the error message
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
console.error("Signup failed:", response.error);
|
|
|
|
}
|
2023-11-29 23:58:43 +00:00
|
|
|
}
|
2023-11-30 03:03:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(formData);
|
2023-11-30 00:14:09 +00:00
|
|
|
|
|
|
|
// Enable the button and remove the spinner after 5 seconds
|
|
|
|
setTimeout(function () {
|
|
|
|
submitBtn.disabled = false;
|
|
|
|
submitBtn.innerHTML = 'Sign Up';
|
2023-11-30 03:10:57 +00:00
|
|
|
}, 10000);
|
|
|
|
sleep(10000).then(() => { submitBtn.disabled = false; });
|
2023-11-29 23:20:25 +00:00
|
|
|
}
|
|
|
|
|
2023-11-29 23:58:43 +00:00
|
|
|
|
|
|
|
|
2023-11-30 03:03:28 +00:00
|
|
|
|
2023-11-29 23:20:25 +00:00
|
|
|
function sleep(ms) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
|
|
|
function submitLogin() {
|
|
|
|
|
|
|
|
// Get the values of the username and password fields
|
|
|
|
const username = loginForm.elements.username.value;
|
|
|
|
const password = loginForm.elements.login_password.value;
|
|
|
|
|
|
|
|
// Create a new FormData object and append the values
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("username", username);
|
|
|
|
formData.append("password", password);
|
|
|
|
// Send the login request as form data
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("POST", "/login", true);
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
if (xhr.status === 200) {
|
|
|
|
// Login successful, redirect to home page
|
|
|
|
console.log("Waiting for redirect...")
|
|
|
|
window.location.href = "/home";
|
|
|
|
} else {
|
|
|
|
// Login failed, display the error message
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
const loginErrorDiv = document.getElementById("login-error");
|
|
|
|
loginErrorDiv.textContent = response.error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.send(formData);
|
|
|
|
// Disable the button to prevent multiple clicks
|
|
|
|
loginButton.disabled = true;
|
|
|
|
|
|
|
|
// Show the spinner
|
|
|
|
const spinner = document.createElement('i');
|
|
|
|
spinner.classList.add('fas', 'fa-spinner', 'fa-spin');
|
|
|
|
loginButton.innerHTML = '';
|
|
|
|
loginButton.appendChild(spinner);
|
|
|
|
|
|
|
|
// Enable the button and remove the spinner after 5 seconds
|
|
|
|
setTimeout(function () {
|
|
|
|
loginButton.disabled = false;
|
|
|
|
loginButton.innerHTML = 'Login';
|
|
|
|
}, 5000);
|
|
|
|
sleep(5000).then(() => { loginButton.disabled = false; });
|
|
|
|
};
|