forked from peas-dev/peas
1
0
Fork 0
peas/packages/web/public/js/signin.js

49 lines
1.3 KiB
JavaScript

const form = document.querySelector("form");
form.addEventListener("submit", async (e) => {
e.preventDefault();
document.getElementsByName("username")[0].disable = "";
document.getElementsByName("password")[0].disable = "";
document.getElementsByName("submit")[0].disable = "";
let error = "";
const infoReq = await fetch("/api/info", {
method: "POST",
cache: "no-store",
});
if (!infoReq.ok) {
error = "通信エラーが発生しました";
}
const infoRes = await infoReq.json();
const username = document.getElementsByName("username")[0].value;
const password = document.getElementsByName("password")[0].value;
let turnstile;
if (infoRes.botprotection.turnstile === true) {
turnstile = document.getElementsByName("cf-turnstile-response")[0].value;
}
const req = await fetch("/api/signin", {
method: "POST",
cache: "no-store",
headers: {
'Content-Type': "application/json",
},
body: JSON.stringify({
username: username,
password: password,
turnstile: turnstile ?? undefined,
}),
});
if (!req.ok) {
error = "通信エラーが発生しました";
}
const res = await req.json();
if (res.success === true) {
location.href = "/home";
} else {
location.replace(`/signin?error=${res.error}`);
}
});