webupdatev1
This commit is contained in:
@@ -261,54 +261,87 @@
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
document
|
||||
.getElementById("loginForm")
|
||||
.addEventListener("submit", async function (e) {
|
||||
const loginForm = document.getElementById("loginForm");
|
||||
const emailInput = document.getElementById("email");
|
||||
const passwordInput = document.getElementById("password");
|
||||
const errorAlert = document.getElementById("errorAlert");
|
||||
const loginBtn = document.getElementById("loginBtn");
|
||||
|
||||
async function handleLogin(e) {
|
||||
if (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}
|
||||
|
||||
const email = document.getElementById("email").value;
|
||||
const password = document.getElementById("password").value;
|
||||
const errorAlert = document.getElementById("errorAlert");
|
||||
const loginBtn = document.getElementById("loginBtn");
|
||||
// Check if form is valid
|
||||
if (!loginForm.checkValidity()) {
|
||||
loginForm.reportValidity();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Disable button during login
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = "Logging in...";
|
||||
errorAlert.classList.remove("show");
|
||||
const email = emailInput.value.trim();
|
||||
const password = passwordInput.value;
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/admin/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
if (!email || !password) {
|
||||
errorAlert.innerHTML =
|
||||
'<i class="bi bi-exclamation-triangle"></i> Please enter both email and password';
|
||||
errorAlert.classList.add("show");
|
||||
return false;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
// Disable button during login
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = "Logging in...";
|
||||
errorAlert.classList.remove("show");
|
||||
|
||||
if (response.ok && data.success) {
|
||||
// Login successful - redirect to dashboard
|
||||
window.location.href = "/admin/dashboard";
|
||||
} else {
|
||||
// Show error
|
||||
errorAlert.innerHTML =
|
||||
'<i class="bi bi-exclamation-triangle"></i> ' +
|
||||
(data.message || "Invalid credentials");
|
||||
errorAlert.classList.add("show");
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = "Login";
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
try {
|
||||
const response = await fetch("/api/admin/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
credentials: "include",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok && data.success) {
|
||||
// Login successful - redirect to dashboard
|
||||
window.location.href = "/admin/dashboard";
|
||||
} else {
|
||||
// Show error
|
||||
errorAlert.innerHTML =
|
||||
'<i class="bi bi-exclamation-triangle"></i> Login failed. Please try again.';
|
||||
'<i class="bi bi-exclamation-triangle"></i> ' +
|
||||
(data.message || "Invalid credentials");
|
||||
errorAlert.classList.add("show");
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = "Login";
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
errorAlert.innerHTML =
|
||||
'<i class="bi bi-exclamation-triangle"></i> Login failed. Please try again.';
|
||||
errorAlert.classList.add("show");
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = "Login";
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle form submission
|
||||
loginForm.addEventListener("submit", handleLogin);
|
||||
|
||||
// Handle Enter key on both fields
|
||||
function handleEnterKey(e) {
|
||||
if (e.key === "Enter" || e.keyCode === 13 || e.which === 13) {
|
||||
handleLogin(e);
|
||||
}
|
||||
}
|
||||
|
||||
emailInput.addEventListener("keydown", handleEnterKey);
|
||||
passwordInput.addEventListener("keydown", handleEnterKey);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user