Added in editor login feature to login into editor

Signed-off-by: rodude123 <rodude123@gmail.com>
This commit is contained in:
2022-07-29 20:00:36 +01:00
parent 315a0484b0
commit 90a3e4f533
31 changed files with 831 additions and 478 deletions
+54
View File
@@ -0,0 +1,54 @@
document.addEventListener("DOMContentLoaded", e =>
{
// check if the user is logged in and if so load the editor
fetch("/api/user/isLoggedIn").then(res =>
{
if (res.ok)
{
window.location.href = "./editor.html";
}
});
});
function showErrorMessage(message)
{
document.querySelector("#loginError").classList.remove("hidden");
document.querySelector("#loginError div").innerText = message;
}
document.querySelector("#login form").addEventListener("submit", e =>
{
e.preventDefault();
let loginData = new FormData();
if (e.target.username.value.length > 0 && e.target.password.value.length > 0)
{
loginData.append("username", e.target.username.value);
loginData.append("password", e.target.password.value);
fetch("/api/user/login",
{
method: "POST",
body: loginData
}).then(res =>
{
if (res.ok)
{
window.location.href = "./editor.html";
return;
}
if (res.status === 400)
{
showErrorMessage("Please type in a username and password.");
return;
}
document.querySelector("#loginError").classList.remove("hidden");
document.querySelector("#loginError div").innerHTML = "Invalid username or password";
});
return;
}
document.querySelector("#loginError").classList.remove("hidden");
document.querySelector("#loginError div").innerHTML = "Please type in a username and password";
});
document.querySelector("#loginError .close").addEventListener("click", () =>
document.querySelector("#loginError").classList.toggle("hidden"));