From 91052e605739001e58ef4b348f9c5c847cbbffbb Mon Sep 17 00:00:00 2001 From: Fingertips Date: Sun, 22 Sep 2024 23:55:51 +0800 Subject: [PATCH 1/2] Create forgot password form --- client/src/App.tsx | 5 ++ client/src/components/or.tsx | 2 +- client/src/components/switch-auth.tsx | 9 ++- client/src/constants/keys.ts | 1 + client/src/lib/DTO/forgot-password-dto.ts | 3 + client/src/lib/services/auth-service.ts | 30 ++++++-- .../_components/forgot-password-form.tsx | 70 +++++++++++++++++++ client/src/pages/forgot-password/page.tsx | 27 +++++++ .../sign-in/_components/sign-in-form.tsx | 14 ++-- client/src/pages/sign-in/page.tsx | 9 ++- 10 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 client/src/lib/DTO/forgot-password-dto.ts create mode 100644 client/src/pages/forgot-password/_components/forgot-password-form.tsx create mode 100644 client/src/pages/forgot-password/page.tsx diff --git a/client/src/App.tsx b/client/src/App.tsx index 5a15f5e..386f2b1 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,5 +1,6 @@ import { Route, Routes } from "react-router-dom"; +import { ForgotPasswordPage } from "@/pages/forgot-password/page"; import VerifyEmailPage from "@/pages/verify-email/page"; import PrivateGuard from "@/guards/private-guard"; import { AppRoutes } from "@/constants/routes"; @@ -20,6 +21,10 @@ function App() { } /> } /> + } + /> ); diff --git a/client/src/components/or.tsx b/client/src/components/or.tsx index 8d7a48a..9929b84 100644 --- a/client/src/components/or.tsx +++ b/client/src/components/or.tsx @@ -1,6 +1,6 @@ const Or = () => { return ( -
+

Or

diff --git a/client/src/components/switch-auth.tsx b/client/src/components/switch-auth.tsx index 55fae3a..d11af44 100644 --- a/client/src/components/switch-auth.tsx +++ b/client/src/components/switch-auth.tsx @@ -1,15 +1,18 @@ import { Link } from "react-router-dom"; interface SwitchAuthProps { - label: string; + label?: string; tag: string; href: string; } const SwitchAuth = ({ label, tag, href }: SwitchAuthProps) => { return ( -
-

{label}

+
+ {label &&

{label}

} { + const res = await fetch(`${baseURL}${AppRoutes.ForgotPassword}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(email), + }); + + const data = await res.json(); + + if (!res.ok) { + throw new ErrorResponse({ + status: res.status, + message: data.error, + }); + } + + return new GenericResponse({ + message: data.message, + }); + }, }; diff --git a/client/src/pages/forgot-password/_components/forgot-password-form.tsx b/client/src/pages/forgot-password/_components/forgot-password-form.tsx new file mode 100644 index 0000000..96161fa --- /dev/null +++ b/client/src/pages/forgot-password/_components/forgot-password-form.tsx @@ -0,0 +1,70 @@ +import { useMutation } from "@tanstack/react-query"; +import { useNavigate } from "react-router-dom"; +import { Mail } from "lucide-react"; +import { FormEvent } from "react"; +import { toast } from "sonner"; + +import { GenericResponse } from "@/lib/classes/generic-response-class"; +import { ErrorResponse } from "@/lib/classes/error-response-class"; +import { ForgotPasswordDTO } from "@/lib/DTO/forgot-password-dto"; +import { AuthService } from "@/lib/services/auth-service"; +import { ValidateEmail } from "@/lib/utils/validations"; +import { FORGOTPASSWORDKEY } from "@/constants/keys"; +import { Button } from "@/components/text-button"; +import { AppRoutes } from "@/constants/routes"; +import { Input } from "@/components/input"; + +const ForgotPasswordForm = () => { + const navigate = useNavigate(); + + const { mutate, isPending } = useMutation({ + mutationKey: [FORGOTPASSWORDKEY], + mutationFn: AuthService.forgotPassword, + onSuccess: (res: GenericResponse) => { + toast.success(res.message); + navigate(AppRoutes.ResetPassword); + }, + onError: (error: ErrorResponse) => toast.error(error.message), + }); + + const onSubmit = (e: FormEvent) => { + e.preventDefault(); + + const formData = new FormData(e.currentTarget); + + const forgotPasswordData = Object.fromEntries( + formData.entries() + ) as ForgotPasswordDTO; + + mutate(forgotPasswordData.email); + }; + + return ( +
+ + +