summaryrefslogtreecommitdiff
path: root/static/login.js
blob: ee135f89e0c2fcd8d9e07ff64c34692514b399d0 (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
37
38

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 {
            console.log();
            
            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;
            }
            
            //const result = await res.json();
            
        } catch (err) {
            console.log(err);
            //display_error("network error");
        }
    });
}