diff --git a/frontend/src/app/(auth)/login/page.tsx b/frontend/src/app/(auth)/login/page.tsx index 14654a2..96214c5 100644 --- a/frontend/src/app/(auth)/login/page.tsx +++ b/frontend/src/app/(auth)/login/page.tsx @@ -1,8 +1,10 @@ "use client"; import React, { useState } from "react"; +import { useRouter } from "next/navigation"; import Image from "next/image"; import Link from "next/link"; +import { userAgentFromString } from "next/server"; export default function Page() { // const [isBlurred, setIsBlurred] = useState(false); @@ -11,6 +13,58 @@ export default function Page() { // setIsBlurred(true); // }; + const [userLogin, setUserLogin] = useState({ + email: "", + password: "", + }); + const [loginError, setLoginError] = useState(""); + const route = useRouter(); + + const fetchUserInfo = async () => { + try { + const res: Response = await fetch("/v1/user/login", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(userLogin), + }); + if (res.ok) { + const userInfo = await res.json(); + return userInfo; + } else { + console.error("Failed to fetch user info:", res.statusText); + return []; + } + } catch (error) { + console.error("Error fetching user info:", error); + return []; + } + }; + + const handleChange = (e: { target: { name: any; value: any } }) => { + const { name, value } = e.target; + setUserLogin({ + ...userLogin, + [name]: value, + }); + }; + + const handleSubmit = async (e: { preventDefault: () => void }) => { + e.preventDefault(); + const info = await fetchUserInfo(); + for (let i = 0; i < info.length; i++) { + if (info[i].email === userLogin.email) { + if (info[i].password === userLogin.password) { + route.push(""); + } else { + setLoginError("Wrong password entered"); + } + } + } + setLoginError("Wrong email entered"); + }; + return (
@@ -37,6 +91,7 @@ export default function Page() { method="post" className="flex flex-col" // onClick={handleFormClick} + onSubmit={handleSubmit} >