فهرست منبع

Add code to register a new user.

Lee Morgan 3 هفته پیش
والد
کامیت
fe60e51ea4
2فایلهای تغییر یافته به همراه43 افزوده شده و 20 حذف شده
  1. 1 0
      env_example.txt
  2. 42 20
      src/routes/register/+page.svelte

+ 1 - 0
env_example.txt

@@ -1 +1,2 @@
 PORT
+PUBLIC_API_URL

+ 42 - 20
src/routes/register/+page.svelte

@@ -1,5 +1,7 @@
 <script>
     import "$lib/global.css";
+    import {PUBLIC_API_URL} from "$env/static/public";
+    import {goto} from "$app/navigation";
     import favicon from "$lib/assets/favicon.png";
 
     let name = $state("");
@@ -8,8 +10,28 @@
     let confirmPassword = $state("");
     let url = $state("");
 
-    const submit = ()=>{
-        console.log("submitting");
+    const submit = async ()=>{
+        const body = {
+            name: name,
+            email: email,
+            password: password,
+            confirm_password: confirmPassword,
+            url: url
+        };
+
+        const response = await fetch(`${PUBLIC_API_URL}/user`, {
+            method: "POST",
+            headers: {"Content-Type": "application/json"},
+            body: JSON.stringify(body)
+        });
+
+        const responseBody = await response.json();
+        if(!response.ok){
+            console.log("error");
+            console.log(responseBody);
+        }else{
+            goto("/login");
+        }
     }
 </script>
 
@@ -22,31 +44,31 @@
     </a>
 
     <div class="form-glow">
-    <div id="register" class="standardForm" onsubmit={submit}>
-        <h2>Join The Adventure</h2>
+        <form class="standardForm" onsubmit={submit}>
+            <h2>Join The Adventure</h2>
 
-        <label>Name
-            <input bind:value={name} type="text" required>
-        </label>
+            <label>Name
+                <input bind:value={name} type="text" required>
+            </label>
 
-        <label>Email
-            <input bind:value={email} type="email" required>
-        </label>
+            <label>Email
+                <input bind:value={email} type="email" required>
+            </label>
 
-        <label>Password
-            <input bind:value={password} type="password" required>
-        </label>
+            <label>Password
+                <input bind:value={password} type="password" required>
+            </label>
 
-        <label>Confirm Password
-            <input bind:value={confirmPassword} type="password" required>
-        </label>
+            <label>Confirm Password
+                <input bind:value={confirmPassword} type="password" required>
+            </label>
 
-        <input bind:value={url} type="hidden" required>
+            <input bind:value={url} type="hidden" required>
 
-        <button type="submit">Register</button>
+            <button type="submit">Register</button>
 
-        <p>Already have an account? <a href="/login">Log in</a></p>
-    </div>
+            <p>Already have an account? <a href="/login">Log in</a></p>
+        </form>
     </div>
 </main>