Refactored forgot password form validation and connected to redux slice

This commit is contained in:
Daniel Cojocea committed 2024-07-16 12:17:18 -04:00
1 parent 1f21212072
commit 0c11473d45
3 files changed
+112 -42

No files matched your search

+35
View File
@@ -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);
},
});