summaryrefslogtreecommitdiff
path: root/static/login.js
blob: 1dd61b743b8f1ca316b5a92daf92ae5946d27a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

function display_error(message) {
    document.getElementById("error-message").innerHTML = `Error: ${message}`;
    document.getElementById("error").hidden = false;
}

function init() {
    const form = document.getElementById("login-form");
    form.addEventListener("submit", async (e) => {
        e.preventDefault();

        const body = {}
        new FormData(form).forEach((value, key) => body[key] = value);

        try {
            const res = await fetch("/login", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                credentials: "include",
                body: JSON.stringify(body)
            });

            if (!res.ok) {
                const error = await res.json();
                display_error(error.error);
                return;
            }

            window.location.href = "index.html";
            
        } catch (err) {
            console.log(err);
            //display_error("network error");
        }
    });
}