From 9e9e520ae4e39d8fd995ab2de25b28e20be254b7 Mon Sep 17 00:00:00 2001 From: mftee Date: Thu, 29 Jan 2026 04:47:17 +0100 Subject: [PATCH] implemented and validated the details for a forgot password page --- frontend/app/forgot-password/page.tsx | 210 ++++++++++++++++++++++++++ 1 file changed, 210 insertions(+) create mode 100644 frontend/app/forgot-password/page.tsx diff --git a/frontend/app/forgot-password/page.tsx b/frontend/app/forgot-password/page.tsx new file mode 100644 index 0000000..7fb8983 --- /dev/null +++ b/frontend/app/forgot-password/page.tsx @@ -0,0 +1,210 @@ +"use client"; + +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { z } from "zod"; +import Link from "next/link"; +import { useRouter } from "next/navigation"; + +const forgotPasswordSchema = z.object({ + email: z.string().email("Please enter a valid email address"), +}); + +type ForgotPasswordForm = z.infer; + +export default function ForgotPasswordPage() { + const [isLoading, setIsLoading] = useState(false); + const [isSuccess, setIsSuccess] = useState(false); + const [submittedEmail, setSubmittedEmail] = useState(""); + const [countdown, setCountdown] = useState(5); + const router = useRouter(); + + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ + resolver: zodResolver(forgotPasswordSchema), + }); + + const onSubmit = async (data: ForgotPasswordForm) => { + setIsLoading(true); + + try { + const response = await fetch("/api/v1/auth/forgot-password", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(data), + }); + + if (!response.ok) { + throw new Error("Failed to send reset email"); + } + + setSubmittedEmail(data.email); + setIsSuccess(true); + + // Start countdown and redirect + let timeLeft = 5; + const timer = setInterval(() => { + timeLeft -= 1; + setCountdown(timeLeft); + if (timeLeft === 0) { + clearInterval(timer); + router.push("/login"); + } + }, 1000); + } catch (error) { + alert("Failed to send reset email. Please try again."); + } finally { + setIsLoading(false); + } + }; + + if (isSuccess) { + return ( +
+
+
+
+ + + +
+

+ Check Your Email +

+

+ We've sent password reset instructions to +

+

+ {submittedEmail} +

+

+ Didn't receive the email? Check your spam folder or{" "} + +

+
+ +
+ + Back to Login + +

+ Redirecting in {countdown} seconds... +

+
+
+
+ ); + } + + return ( +
+
+
+

+ Reset Your Password +

+

+ Enter your email and we'll send you a reset link +

+
+ +
+
+ +
+ + {errors.email && ( +

+ {errors.email.message} +

+ )} +
+
+ +
+ +
+ +
+ + ← Back to Login + +
+
+
+
+ ); +}