From c3001d8b8f3ffe03b4a470a863374540c868a477 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 26 Feb 2025 17:44:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=20register=20integration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/components-auth-login-form.tsx | 4 +- .../auth/components-auth-register-form.tsx | 39 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/components/auth/components-auth-login-form.tsx b/components/auth/components-auth-login-form.tsx index ac1f086..77cf895 100644 --- a/components/auth/components-auth-login-form.tsx +++ b/components/auth/components-auth-login-form.tsx @@ -14,8 +14,8 @@ const ComponentsAuthLoginForm = () => { const submitForm = async (e: React.FormEvent) => { e.preventDefault() - setLoading(true) + setLoading(true) try { const res = await axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/login`, { email, @@ -32,7 +32,7 @@ const ComponentsAuthLoginForm = () => { } finally { setLoading(false) } - }; + } return (
diff --git a/components/auth/components-auth-register-form.tsx b/components/auth/components-auth-register-form.tsx index 0a47cd5..6db417a 100644 --- a/components/auth/components-auth-register-form.tsx +++ b/components/auth/components-auth-register-form.tsx @@ -2,15 +2,38 @@ import IconLockDots from '@/components/icon/icon-lock-dots'; import IconMail from '@/components/icon/icon-mail'; import IconUser from '@/components/icon/icon-user'; +import axios from 'axios'; import { useRouter } from 'next/navigation'; +import { useState } from "react"; import React from 'react'; +import toast from 'react-hot-toast'; const ComponentsAuthRegisterForm = () => { - const router = useRouter(); + const [email, setEmail] = useState("") + const [password, setPassword] = useState("") + const [loading, setLoading] = useState(false) + const router = useRouter() - const submitForm = (e: any) => { - e.preventDefault(); - router.push('/'); + const submitForm = async(e: any) => { + e.preventDefault() + + setLoading(true) + try { + const res = await axios.post(`${process.env.NEXT_PUBLIC_API_BASE_URL}/api/register`, { + email, + password, + }) + + localStorage.setItem("token", res.data.token) + + toast.success("Register successful!") + router.push("/") + } catch (err: any) { + console.error("Register error:", err) + toast.error(err.response?.data?.error || "Something went wrong") + } finally { + setLoading(false) + } }; return ( @@ -26,7 +49,7 @@ const ComponentsAuthRegisterForm = () => {
- + setEmail(e.target.value)} type="email" placeholder="Enter Email" className="form-input ps-10 placeholder:text-white-dark" /> @@ -35,14 +58,14 @@ const ComponentsAuthRegisterForm = () => {
- + setPassword(e.target.value)} type="password" placeholder="Enter Password" className="form-input ps-10 placeholder:text-white-dark" />
- );