diff --git a/Client/src/Features/Auth/authSlice.js b/Client/src/Features/Auth/authSlice.js index 15dd735b1..0e241bbcf 100644 --- a/Client/src/Features/Auth/authSlice.js +++ b/Client/src/Features/Auth/authSlice.js @@ -96,6 +96,21 @@ export const deleteUser = createAsyncThunk( } ); +export const forgotPassword = createAsyncThunk( + "auth/forgotPassword", + async (form, thunkApi) => { + try { + const res = await axiosInstance.post("/auth/recovery/request", form); + return res.data; + } catch (error) { + if (error.response.data) { + return thunkApi.rejectWithValue(error.response.data); + } + return thunkApi.rejectWithValue(error.message); + } + } +); + const handleAuthFulfilled = (state, action) => { state.isLoading = false; state.success = action.payload.success; @@ -133,6 +148,18 @@ const handleDeleteRejected = (state, action) => { state.success = false; state.msg = action.payload ? action.payload.msg : "Failed to delete account."; }; +const handleForgotPasswordFulfilled = (state, action) => { + state.isLoading = false; + state.success = action.payload.success; + state.msg = action.payload.msg; +}; +const handleForgotPasswordRejected = (state, action) => { + state.isLoading = false; + state.success = false; + state.msg = action.payload + ? action.payload.msg + : "Failed to send reset instructions."; +}; const authSlice = createSlice({ name: "auth", @@ -178,6 +205,14 @@ const authSlice = createSlice({ }) .addCase(deleteUser.fulfilled, handleDeleteFulfilled) .addCase(deleteUser.rejected, handleDeleteRejected); + + // Forgot password thunk + builder + .addCase(forgotPassword.pending, (state) => { + state.isLoading = true; + }) + .addCase(forgotPassword.fulfilled, handleForgotPasswordFulfilled) + .addCase(forgotPassword.rejected, handleForgotPasswordRejected); }, }); diff --git a/Client/src/Pages/ForgotPassword/index.css b/Client/src/Pages/ForgotPassword/index.css index acb24e6e9..42600a77b 100644 --- a/Client/src/Pages/ForgotPassword/index.css +++ b/Client/src/Pages/ForgotPassword/index.css @@ -1,8 +1,7 @@ .forgot-password-page { - width: var(--env-var-width-1); display: flex; justify-content: center; - min-height: var(--env-var-height-1); + height: var(--env-var-height-1); } .forgot-password-form { diff --git a/Client/src/Pages/ForgotPassword/index.jsx b/Client/src/Pages/ForgotPassword/index.jsx index 839e6a882..80bda6d4c 100644 --- a/Client/src/Pages/ForgotPassword/index.jsx +++ b/Client/src/Pages/ForgotPassword/index.jsx @@ -2,18 +2,21 @@ import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPatt import "./index.css"; import React from "react"; import Logomark from "../../assets/Images/key-password.png"; -import Button from "../../Components/Button"; import LeftArrow from "../../assets/Images/arrow-left.png"; -import { useState, useEffect } from "react"; -import { recoveryValidation } from "../../Validation/validation"; -import axiosInstance from "../../Utils/axiosConfig"; +import { useState } from "react"; +import { credentials } from "../../Validation/validation"; import { useNavigate } from "react-router-dom"; import Field from "../../Components/Inputs/Field"; +import { createToast } from "../../Utils/toastUtils"; +import { useDispatch, useSelector } from "react-redux"; +import { forgotPassword } from "../../Features/Auth/authSlice"; +import ButtonSpinner from "../../Components/ButtonSpinner"; const ForgotPassword = () => { const navigate = useNavigate(); + const dispatch = useDispatch(); - const [isLoading, setIsLoading] = useState(false); // Used to disable the button while loading so user doesn't submit multiple times + const { isLoading } = useSelector((state) => state.auth); const [errors, setErrors] = useState({}); const [form, setForm] = useState({ email: "", @@ -23,40 +26,73 @@ const ForgotPassword = () => { "forgot-password-email-input": "email", }; - useEffect(() => { - const { error } = recoveryValidation.validate(form); - if (error === undefined) { - setErrors({}); + const handleSubmit = async (event) => { + event.preventDefault(); + + const { error } = credentials.validate(form, { abortEarly: false }); + + if (error) { + // validation errors + const err = + error.details && error.details.length > 0 + ? error.details[0].message + : "Error validating data."; + setErrors(err); + createToast({ + variant: "info", + body: err, + hasIcon: false, + }); } else { - const validationErrors = error.details.reduce((acc, err) => { - return { ...acc, [err.path[0]]: err.message }; - }, {}); - setErrors(validationErrors); - } - }, [form]); - - const handleInput = (e) => { - const fieldName = idMap[e.target.id]; - setForm({ ...form, [fieldName]: e.target.value }); - }; - - const handleSubmit = async () => { - //TODO show loading spinner - setIsLoading(true); - try { - const { error } = recoveryValidation.validate(form); - if (error !== undefined) { - throw error; + const action = await dispatch(forgotPassword(form)); + if (action.payload.success) { + navigate("/check-email"); + createToast({ + variant: "info", + body: `Instructions sent to ${form.email}.`, + hasIcon: false, + }); + } else { + if (action.payload) { + // dispatch errors + createToast({ + variant: "info", + body: action.payload.msg, + hasIcon: false, + }); + } else { + // unknown errors + createToast({ + variant: "info", + body: "Unknown error.", + hasIcon: false, + }); + } } - await axiosInstance.post(`/auth/recovery/request`, form); - navigate("/check-email"); - } catch (error) { - //TODO display error (Toast?) - alert(error); - } finally { - setIsLoading(false); } }; + + const handleChange = (event) => { + const { value, id } = event.target; + const name = idMap[id]; + setForm((prev) => ({ + ...prev, + [name]: value, + })); + + const { error } = credentials.validate( + { [name]: value }, + { abortEarly: false } + ); + + setErrors((prev) => { + const prevErrors = { ...prev }; + if (error) prevErrors[name] = error.details[0].message; + else delete prevErrors[name]; + return prevErrors; + }); + }; + return (