move to generalized auth instead of login register page
This commit is contained in:
parent
17d7e4f238
commit
99f170750c
6 changed files with 179 additions and 93 deletions
|
@ -1,19 +1,38 @@
|
|||
const loginForm = document.getElementById("login-form");
|
||||
const errorMessage = document.getElementById("error-message");
|
||||
const rememberMe = document.getElementById("remember-me");
|
||||
const emailInput = document.getElementById("email");
|
||||
|
||||
if (emailInput && localStorage.getItem("email")) {
|
||||
emailInput.value = localStorage.getItem("email");
|
||||
}
|
||||
|
||||
if (loginForm) {
|
||||
loginForm.addEventListener("submit", async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const email = document.getElementById("email").value;
|
||||
const password = document.getElementById("password").value;
|
||||
const email = emailInput?.value.trim();
|
||||
const password = document.getElementById("password")?.value.trim();
|
||||
|
||||
if (!email || !password) {
|
||||
if (errorMessage) {
|
||||
errorMessage.style.display = "block";
|
||||
errorMessage.textContent =
|
||||
"Please enter both email and password.";
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (rememberMe?.checked) {
|
||||
localStorage.setItem("email", email);
|
||||
} else {
|
||||
sessionStorage.setItem("email", email);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
headers: { "Content-Type": "application/json" },
|
||||
credentials: "same-origin",
|
||||
body: JSON.stringify({ email, password }),
|
||||
});
|
||||
|
@ -23,15 +42,20 @@ if (loginForm) {
|
|||
if (data.success) {
|
||||
window.location.href = "/";
|
||||
} else {
|
||||
errorMessage.style.display = "block";
|
||||
errorMessage.textContent =
|
||||
data.error ||
|
||||
"Invalid email or password. Please try again.";
|
||||
if (errorMessage) {
|
||||
errorMessage.style.display = "block";
|
||||
errorMessage.textContent =
|
||||
data.error ||
|
||||
"Invalid email or password. Please try again.";
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
errorMessage.style.display = "block";
|
||||
errorMessage.textContent = "An error occurred. Please try again.";
|
||||
if (errorMessage) {
|
||||
errorMessage.style.display = "block";
|
||||
errorMessage.textContent =
|
||||
"An error occurred. Please try again.";
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue