-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
60 lines (56 loc) · 1.82 KB
/
client.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
56
57
58
59
60
document.addEventListener("DOMContentLoaded", function () {
const loginForm = document.getElementById("loginForm");
const userInput = document.getElementById("username");
const passwordInput = document.getElementById("password");
const swappedModal = document.getElementById("modalSwapped");
// Function to show modal
function showModal() {
swappedModal.style.display = "block";
}
// Function to send data to the server
async function sendData(endpoint, data) {
try {
const response = await fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
const errorData = await response.json();
console.error(`Error during data request to ${endpoint}:`, errorData);
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
} catch (error) {
console.error(`Error during data request to ${endpoint}:`, error);
throw error;
}
}
// Login handler
if (loginForm) {
loginForm.addEventListener("submit", async (event) => {
event.preventDefault();
const username = userInput.value.trim();
const password = passwordInput.value.trim();
try {
const loginResponse = await sendData("/login", {
username,
password,
});
if (loginResponse.message == "SIM Swapped") {
showModal(); // Show warning modal if SIM swapped
}
else if (loginResponse.message !== "Success") {
alert("Invalid username or password.");
} else {
window.location.href = "/main";
}
} catch (error) {
console.error("Error during login:", error);
alert("Login process failed.");
}
});
}
});