-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscripts.js
More file actions
52 lines (47 loc) · 1.66 KB
/
Copy pathscripts.js
File metadata and controls
52 lines (47 loc) · 1.66 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
function setCookie(name, value, minutes) {
var expires = "";
if (minutes) {
var date = new Date();
date.setTime(date.getTime() + minutes * 60 * 60 * 1000);
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + value + expires + "; path=/";
}
function authenticate(event) {
event.preventDefault(); // Prevent the default form submission behavior
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
// Make an API request for authentication
fetch("https://garbage-collect-backend.onrender.com/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username: username,
password: password,
}),
})
.then(response => response.json())
.then(data => {
if (data.message === "Login successful") {
// Set a cookie for successful login that expires in 1 minute
setCookie("authToken", "your_token", 12);
// Redirect to www.google.com
window.location.href = "./pages/AdminPage.html";
} else {
alert("Invalid credentials");
}
})
.catch(error => {
console.error("Error:", error);
});
}
// Check if the user is already logged in (e.g., on page load)
window.onload = function () {
var authToken = getCookie("authToken");
if (authToken) {
// User is logged in, redirect to www.google.com
window.location.href = "./pages/AdminPage.html";
}
};