diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index 7786faf..1403733 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -8,6 +8,7 @@ import NotFoundPage from "./pages/NotFoundPage"; import UserPage from "./pages/UserPage"; import ResetPasswordPage from "./pages/ResetPasswordPage"; import CategoryPage from "./pages/CategoryPage"; +import ForgotPasswordForm from "./components/Auth/ForgotPasswordForm"; function App() { const [isLoggedIn, setIsLoggedIn] = useState(false); @@ -43,6 +44,7 @@ function App() { } /> } /> }> + }> }> } /> diff --git a/frontend/src/components/Auth/ForgotPasswordForm.tsx b/frontend/src/components/Auth/ForgotPasswordForm.tsx new file mode 100644 index 0000000..0656307 --- /dev/null +++ b/frontend/src/components/Auth/ForgotPasswordForm.tsx @@ -0,0 +1,91 @@ +import React, { useState } from "react"; +import Input from "../Layout/Input"; +import Button from "../Layout/Button"; + +interface ForgotPasswordProps { + email?: string; +} + +const ForgotPasswordForm: React.FC = () => { + const [email, setEmail] = useState(""); + const [errors, setErrors] = useState({}); + + const confirmPasswordReset = () => { + alert(`Email has been sent`); + + }; + + const handleEmailChange = (e: React.ChangeEvent) => { + setEmail(e.target.value); + if (errors.email) { + setErrors((prev) => ({ ...prev, email: undefined })); + } + }; + + + const validateEmail = (): boolean => { + const newErrors: ForgotPasswordProps = {}; + if (!email) { + newErrors.email = "Email is required."; + } else if (!/\S+@\S+\.\S+/.test(email)) { + newErrors.email = "Please enter a valid email address."; + } + setErrors(newErrors); + return Object.keys(newErrors).length === 0; + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + if (validateEmail()) { + try { + const response = await fetch(`/api/user/forgot_password/${email}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + credentials: "include", + }); + + if (!response.ok) { + const data = await response.json(); + throw new Error(data.message || "An error has occurred while resetting"); + } else { + confirmPasswordReset(); + } + } catch (error: any) { + console.error("Password reset error:", error.message); + setErrors((prev) => ({ + ...prev, + general: error.message || "An unexpected error occurred.", + + })); + } + } + }; + + return ( +
+
+

Forgot Password

+
+ + + {errors.email &&

{errors.email}

} + + +
+
+
+ ); + +}; + +export default ForgotPasswordForm; diff --git a/frontend/src/components/Auth/PasswordResetForm.tsx b/frontend/src/components/Auth/PasswordResetForm.tsx index 5e1f36a..a5f8062 100644 --- a/frontend/src/components/Auth/PasswordResetForm.tsx +++ b/frontend/src/components/Auth/PasswordResetForm.tsx @@ -29,7 +29,7 @@ const PasswordResetForm: React.FC = ({ onSubmit, token }) => { const confirmPasswordReset = () => { alert(`${resetData.newPassword} - ${token}`); - // You can replace this with navigation or API success handling logic + }; const handlePasswordChange = (e: React.ChangeEvent) => { diff --git a/frontend/src/pages/ResetPasswordPage.tsx b/frontend/src/pages/ResetPasswordPage.tsx index ad266b7..a7fdff6 100644 --- a/frontend/src/pages/ResetPasswordPage.tsx +++ b/frontend/src/pages/ResetPasswordPage.tsx @@ -9,6 +9,9 @@ const ResetPasswordPage: React.FC = () => { const handlePasswordReset = () => { }; + if (!token) { + return

Invalid or missing token.

; + } return (