Repository navigation
Expand file tree
/
Copy pathcommon.js
More file actions
71 lines (58 loc) · 2.23 KB
/
Copy pathcommon.js
File metadata and controls
71 lines (58 loc) · 2.23 KB
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
61
62
63
64
65
66
67
68
69
70
71
// Chenxuan Jin 261074669
document.addEventListener("DOMContentLoaded", function () {
const loginButton = document.getElementById("login-button");
const logoutButton = document.getElementById("logout-button");
const recentLink = document.getElementById("recent-link");
let isLoggedIn = false;
function checkLoginStatus() {
return fetch("check_session.php", { cache: "no-store" })
.then((response) => response.json())
.then((data) => {
isLoggedIn = data.loggedIn;
return isLoggedIn;
})
.catch((error) => {
console.error("Error checking login status:", error);
return false;
});
}
function initializeLoginButton() {
checkLoginStatus().then((loggedIn) => {
if (loggedIn) {
loginButton.textContent = "Logged";
loginButton.style.pointerEvents = "none";
} else {
loginButton.textContent = "Login";
loginButton.style.pointerEvents = "auto";
}
});
}
if (logoutButton) {
if (!logoutButton.hasAttribute("data-listener-added")) {
logoutButton.addEventListener("click", function (e) {
e.preventDefault();
fetch("logout.php", { cache: "no-store" })
.then((response) => response.json())
.then((data) => {
if (data.loggedOut) {
alert("You have been logged out.");
window.location.reload();
}
})
.catch((error) => console.error("Error during logout:", error));
});
logoutButton.setAttribute("data-listener-added", "true");
}
}
if (recentLink) {
recentLink.addEventListener("click", async function (event) {
const loggedIn = await checkLoginStatus();
if (!loggedIn) {
event.preventDefault();
alert("You need to log in to access this page.");
window.location.href = "./index.html";
}
});
}
initializeLoginButton();
});