const set_cookie = (name, value, days) => { const expires = new Date(Date.now() + days * 86400 * 1000).toUTCString(); document.cookie = `${name}=${value}; expires=${expires}; path=/;`; } window.onload = async () => { alert("This service requests a captcha from https://ofd.ru and sends an example receipt to it to check the correctness of the captcha. If you are not okay with making such requests, please leave the site immediately"); if (document.cookie.includes('JWT')) { document.location = '/'; } const form = document.getElementById("loginForm"); const username = document.getElementById("username"); const password = document.getElementById("password"); form.addEventListener('submit', async (e) => { e.preventDefault(); const response = await fetch( `/api/user/login`, { method: "POST", headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ "username": username.value, "password": password.value }) }); switch (response.status) { case 403: alert("Incorrect password"); password.value = ""; break; case 200: response_json = await response.json() set_cookie("JWT", response_json["token"], 365); window.location.href = '/'; break; case 404: alert("No such user exists"); username.value = ""; break; default: alert("Unknown server error. Please, conact the developer"); console.log(response); } }); };