File size: 1,266 Bytes
b82f276
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
document.addEventListener("DOMContentLoaded", () => {
    const form = document.getElementById("registerForm");
    if (!form) return;

    form.addEventListener("submit", handleSubmit);

    async function handleSubmit(e) {
        e.preventDefault();

        const getValue = id => document.getElementById(id)?.value.trim();

        const user = {
            name: getValue("name"),
            email: getValue("email"),
            password: getValue("password")
        };

        if (!user.name || !user.email || !user.password) {
            alert("Please fill all fields");
            return;
        }

        try {
            const response = await fetch("/api/register", {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify(user)
            });

            const data = await response.json();

            if (!response.ok) throw new Error(data.message || "Registration failed");

            alert("Success! Redirecting...");
            setTimeout(() => window.location.href = "login.html", 500);

        } catch (error) {
            console.error("Error:", error);
            alert(error.message);
        }
    }
});