Update script.js
Browse files
script.js
CHANGED
|
@@ -1,7 +1,41 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
"
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
}
|
|
|
|
| 1 |
+
// Redirect if not logged in
|
| 2 |
+
if (window.location.pathname.includes("app.html")) {
|
| 3 |
+
if (!localStorage.getItem("loggedInUser")) {
|
| 4 |
+
window.location.href = "index.html";
|
| 5 |
+
}
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
// Signup
|
| 9 |
+
function signup() {
|
| 10 |
+
const user = username.value;
|
| 11 |
+
const pass = password.value;
|
| 12 |
+
|
| 13 |
+
if (!user || !pass) {
|
| 14 |
+
message.innerText = "Please fill all fields";
|
| 15 |
+
return;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
localStorage.setItem(user, pass);
|
| 19 |
+
message.innerText = "Signup successful! You can now login.";
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// Login
|
| 23 |
+
function login() {
|
| 24 |
+
const user = username.value;
|
| 25 |
+
const pass = password.value;
|
| 26 |
+
|
| 27 |
+
const storedPass = localStorage.getItem(user);
|
| 28 |
+
|
| 29 |
+
if (storedPass === pass) {
|
| 30 |
+
localStorage.setItem("loggedInUser", user);
|
| 31 |
+
window.location.href = "app.html";
|
| 32 |
+
} else {
|
| 33 |
+
message.innerText = "Invalid credentials ❌";
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Logout
|
| 38 |
+
function logout() {
|
| 39 |
+
localStorage.removeItem("loggedInUser");
|
| 40 |
+
window.location.href = "index.html";
|
| 41 |
}
|