From a4685927a1f7fe8374a567ee74cc62aad639f4c3 Mon Sep 17 00:00:00 2001
From: nsfisis <nsfisis@gmail.com>
Date: Mon, 22 Jul 2024 04:36:45 +0900
Subject: user login

---
 frontend/src/routes/Login.tsx | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

(limited to 'frontend/src/routes/Login.tsx')

diff --git a/frontend/src/routes/Login.tsx b/frontend/src/routes/Login.tsx
index 1945abe..f3fa00e 100644
--- a/frontend/src/routes/Login.tsx
+++ b/frontend/src/routes/Login.tsx
@@ -1,4 +1,4 @@
-import { Form } from "react-router-dom";
+import { redirect, Form, ActionFunctionArgs } from "react-router-dom";
 
 export default function Login() {
   return (
@@ -17,3 +17,22 @@ export default function Login() {
     </div>
   );
 };
+
+export async function loginAction({ request }: ActionFunctionArgs) {
+  const formData = await request.formData();
+  const username = formData.get("username");
+  const password = formData.get("password");
+
+  const res = await fetch("/api/login", {
+    method: "POST",
+    headers: {
+      "Content-Type": "application/json",
+    },
+    body: JSON.stringify({ username, password }),
+  });
+  if (!res.ok) {
+    throw res;
+  }
+  const { userId } = await res.json();
+  return redirect(`/users/${userId}/`);
+};
-- 
cgit v1.2.3-70-g09d2

