ready for PR
This commit is contained in:
108
www/js/auth/auth.js
Normal file
108
www/js/auth/auth.js
Normal file
@@ -0,0 +1,108 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Function to generate a random token
|
||||
function generateAuthToken(length = 32) {
|
||||
const charset =
|
||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let token = "";
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * charset.length);
|
||||
token += charset[randomIndex];
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
// Initially hide the body to prevent content from flashing
|
||||
document.body.style.display = "none";
|
||||
|
||||
// Check if the user is already logged in
|
||||
const authToken = localStorage.getItem("authToken");
|
||||
|
||||
// Define which pages should be protected
|
||||
const protectedPages = [
|
||||
"/home.html",
|
||||
"advance-settings.html",
|
||||
"/bandlock.html",
|
||||
"/cell-locking.html",
|
||||
"/cell-scanner.html",
|
||||
"/cell-settings.html",
|
||||
"/cell-sms.html",
|
||||
"/about.html", // Add all the protected HTML pages here
|
||||
];
|
||||
|
||||
const currentPage = window.location.pathname;
|
||||
|
||||
// If the user is not logged in and tries to access a protected page, redirect to login
|
||||
if (!authToken && protectedPages.includes(currentPage)) {
|
||||
window.location.href = "index.html";
|
||||
} else {
|
||||
// Show the page if authentication is successful or not required
|
||||
document.body.style.display = "";
|
||||
}
|
||||
|
||||
// If the user is logged in and tries to access the login page, redirect to home
|
||||
if (authToken && currentPage.includes("index.html")) {
|
||||
window.location.href = "home.html";
|
||||
}
|
||||
|
||||
// Login form logic (only for login page)
|
||||
const loginForm = document.getElementById("loginForm");
|
||||
if (loginForm) {
|
||||
loginForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const username = document.getElementById("username").value;
|
||||
const password = document.getElementById("password").value;
|
||||
const errorElement = document.getElementById("error");
|
||||
|
||||
try {
|
||||
const formData = new URLSearchParams();
|
||||
formData.append("username", username);
|
||||
formData.append("password", encodeURIComponent(password)); // URL-encode the password
|
||||
|
||||
const response = await fetch("/cgi-bin/auth.sh", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
|
||||
const result = await response.json(); // Parse JSON response
|
||||
|
||||
if (result.state === "success") {
|
||||
const newToken = generateAuthToken();
|
||||
localStorage.setItem("authToken", newToken); // Store the token
|
||||
window.location.href = "home.html"; // Redirect on success
|
||||
} else {
|
||||
document.getElementById("error").textContent =
|
||||
"Invalid username or password";
|
||||
console.log("Invalid username or password");
|
||||
}
|
||||
} catch (error) {
|
||||
// Handle any errors (e.g., network issues)
|
||||
errorElement.textContent = "An error occurred. Please try again later.";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Logout button logic (only for pages that have the logout button)
|
||||
const logoutButton = document.getElementById("logoutButton");
|
||||
if (logoutButton) {
|
||||
logoutButton.addEventListener("click", () => {
|
||||
localStorage.removeItem("authToken"); // Remove token
|
||||
window.location.href = "index.html"; // Redirect to login
|
||||
});
|
||||
}
|
||||
|
||||
// Fix for the issue of being redirected to login every time the Home button is clicked
|
||||
document.querySelectorAll(".navbar-item").forEach((el) => {
|
||||
if (el.textContent.includes("Home")) {
|
||||
el.addEventListener("click", (e) => {
|
||||
if (localStorage.getItem("authToken")) {
|
||||
e.preventDefault();
|
||||
window.location.href = "home.html";
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
36
www/js/handle-at-input.js
Normal file
36
www/js/handle-at-input.js
Normal file
@@ -0,0 +1,36 @@
|
||||
// Handle form submission via JavaScript
|
||||
document
|
||||
.getElementById("commandForm")
|
||||
.addEventListener("submit", function (e) {
|
||||
e.preventDefault(); // Prevent default form submission
|
||||
|
||||
const commandInput = document.getElementById("command").value;
|
||||
const outputTextarea = document.getElementById("output");
|
||||
|
||||
// Make sure input is not empty
|
||||
if (commandInput.trim() === "") {
|
||||
outputTextarea.value = "Please enter a valid AT command.";
|
||||
return;
|
||||
}
|
||||
|
||||
// Send the AT command to the CGI script via fetch
|
||||
fetch("/cgi-bin/atinout_handler.sh", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
body: `command=${encodeURIComponent(commandInput)}`,
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
// Display the response in the textarea
|
||||
if (data.output) {
|
||||
outputTextarea.value = data.output;
|
||||
} else {
|
||||
outputTextarea.value = "Error: No output received.";
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
outputTextarea.value = `Error fetching data: ${error.message}`;
|
||||
});
|
||||
});
|
||||
1033
www/js/home/main.js
Normal file
1033
www/js/home/main.js
Normal file
File diff suppressed because it is too large
Load Diff
17
www/js/styles/nav-burger.js
Normal file
17
www/js/styles/nav-burger.js
Normal file
@@ -0,0 +1,17 @@
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
// Get all "navbar-burger" elements
|
||||
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
|
||||
|
||||
// Add a click event on each of them
|
||||
$navbarBurgers.forEach(el => {
|
||||
el.addEventListener('click', () => {
|
||||
// Get the target from the "data-target" attribute
|
||||
const target = el.dataset.target;
|
||||
const $target = document.getElementById(target);
|
||||
|
||||
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
|
||||
el.classList.toggle('is-active');
|
||||
$target.classList.toggle('is-active');
|
||||
});
|
||||
});
|
||||
});
|
||||
11
www/js/styles/nav-state.js
Normal file
11
www/js/styles/nav-state.js
Normal file
@@ -0,0 +1,11 @@
|
||||
$(document).ready(function() {
|
||||
|
||||
// Check for click events on the navbar burger icon
|
||||
$(".navbar-burger").click(function() {
|
||||
|
||||
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
|
||||
$(".navbar-burger").toggleClass("is-active");
|
||||
$(".navbar-menu").toggleClass("is-active");
|
||||
|
||||
});
|
||||
});
|
||||
27
www/js/styles/toggle-theme.js
Normal file
27
www/js/styles/toggle-theme.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// toggle-theme.js
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const themeToggleButton = document.querySelector('.js-theme-toggle');
|
||||
const htmlElement = document.documentElement;
|
||||
const icon = themeToggleButton.querySelector('.icon i');
|
||||
|
||||
// Toggle theme on button click
|
||||
themeToggleButton.addEventListener('click', function () {
|
||||
if (htmlElement.classList.contains('theme-dark')) {
|
||||
htmlElement.classList.remove('theme-dark');
|
||||
htmlElement.classList.add('theme-light');
|
||||
localStorage.setItem('theme', 'theme-light');
|
||||
|
||||
// Change icon to moon (light mode)
|
||||
icon.classList.remove('fa-sun');
|
||||
icon.classList.add('fa-moon');
|
||||
} else {
|
||||
htmlElement.classList.remove('theme-light');
|
||||
htmlElement.classList.add('theme-dark');
|
||||
localStorage.setItem('theme', 'theme-dark');
|
||||
|
||||
// Change icon to sun (dark mode)
|
||||
icon.classList.remove('fa-moon');
|
||||
icon.classList.add('fa-sun');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user