generated from cis3296f22/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signup.js
55 lines (51 loc) · 1.97 KB
/
signup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Function to validate signup by getting the new username & new password, based on response display any errors/messages front end
*/
function validateSignup() {
var newUsername = document.getElementById("newUsername").value;
var newPassword = document.getElementById("newPassword").value;
const hashedPassword = CryptoJS.SHA256(newPassword).toString(); // Fixed variable name typo (password -> newPassword)
var signupError = document.getElementById("signup-error");
fetch('http://localhost:5002/signup', {
method: "POST",
headers: {
"Content-Type": "application/json;",
},
body: JSON.stringify({ "newUsername": newUsername, "hashedPassword": hashedPassword }),
})
.then((response) => {
console.log(response);
if (response.status === 401) {
throw new Error("Unauthorized");
} else if (!response.ok) {
throw new Error("Signup failed");
}
return response.json();
})
.then((data) => {
// Handle response from the server
if (data.success) {
// Signup was successful
console.log("Account Creation successful");
signupError.textContent = "Account creation successful";
signupError.style.color = "green";
} else {
// Signup failed
console.error("Account Creation failed");
signupError.textContent = "Account creation failed";
signupError.style.color = "red";
}
})
.catch((error) => {
console.error("Error:", error);
// Handle other errors, such as network issues
signupError.textContent = `Something went wrong, please try again. Reason: ${error.message} : Username Exists`;
signupError.style.color = "red";
});
}
document
.getElementById("signupForm")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent the default form submission
validateSignup();
});