Refactored set new password form validation and connected to redux slice
This commit is contained in:
1 parent
3c8e722706
commit
0df0436ce2
2 files changed
+138
-49
No files matched your search
@@ -111,6 +111,28 @@ export const forgotPassword = createAsyncThunk(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const setNewPassword = createAsyncThunk(
|
||||||
|
"auth/setNewPassword",
|
||||||
|
async (data, thunkApi) => {
|
||||||
|
const { token, form } = data;
|
||||||
|
try {
|
||||||
|
await axiosInstance.post("/auth/recovery/validate", {
|
||||||
|
recoveryToken: token,
|
||||||
|
});
|
||||||
|
const res = await axiosInstance.post("/auth/recovery/reset", {
|
||||||
|
...form,
|
||||||
|
recoveryToken: token,
|
||||||
|
});
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response.data) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
return thunkApi.rejectWithValue(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const handleAuthFulfilled = (state, action) => {
|
const handleAuthFulfilled = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.success = action.payload.success;
|
state.success = action.payload.success;
|
||||||
@@ -148,12 +170,24 @@ const handleDeleteRejected = (state, action) => {
|
|||||||
state.success = false;
|
state.success = false;
|
||||||
state.msg = action.payload ? action.payload.msg : "Failed to delete account.";
|
state.msg = action.payload ? action.payload.msg : "Failed to delete account.";
|
||||||
};
|
};
|
||||||
const handleForgotPasswordFulfilled = (state, action) => {
|
const handleForgotFulfilled = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.success = action.payload.success;
|
state.success = action.payload.success;
|
||||||
state.msg = action.payload.msg;
|
state.msg = action.payload.msg;
|
||||||
};
|
};
|
||||||
const handleForgotPasswordRejected = (state, action) => {
|
const handleForgotRejected = (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = false;
|
||||||
|
state.msg = action.payload
|
||||||
|
? action.payload.msg
|
||||||
|
: "Failed to send reset instructions.";
|
||||||
|
};
|
||||||
|
const handleNewPasswordFulfilled = (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
};
|
||||||
|
const handleNewPasswordRejected = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.success = false;
|
state.success = false;
|
||||||
state.msg = action.payload
|
state.msg = action.payload
|
||||||
@@ -211,8 +245,16 @@ const authSlice = createSlice({
|
|||||||
.addCase(forgotPassword.pending, (state) => {
|
.addCase(forgotPassword.pending, (state) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(forgotPassword.fulfilled, handleForgotPasswordFulfilled)
|
.addCase(forgotPassword.fulfilled, handleForgotFulfilled)
|
||||||
.addCase(forgotPassword.rejected, handleForgotPasswordRejected);
|
.addCase(forgotPassword.rejected, handleForgotRejected);
|
||||||
|
|
||||||
|
// Set new password thunk
|
||||||
|
builder
|
||||||
|
.addCase(setNewPassword.pending, (state) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(setNewPassword.fulfilled, handleNewPasswordFulfilled)
|
||||||
|
.addCase(setNewPassword.rejected, handleNewPasswordRejected);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,72 +2,116 @@ import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPatt
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
import LockIcon from "../../assets/Images/lock-icon.png";
|
import LockIcon from "../../assets/Images/lock-icon.png";
|
||||||
import Check from "../../Components/Check/Check";
|
import Check from "../../Components/Check/Check";
|
||||||
import Button from "../../Components/Button";
|
import ButtonSpinner from "../../Components/ButtonSpinner";
|
||||||
import LeftArrow from "../../assets/Images/arrow-left.png";
|
import LeftArrow from "../../assets/Images/arrow-left.png";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { useState, useEffect } from "react";
|
import { useState } from "react";
|
||||||
import { newPasswordValidation } from "../../Validation/validation";
|
import { credentials } from "../../Validation/validation";
|
||||||
import axiosInstance from "../../Utils/axiosConfig";
|
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import Field from "../../Components/Inputs/Field";
|
import Field from "../../Components/Inputs/Field";
|
||||||
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
|
import { setNewPassword } from "../../Features/Auth/authSlice";
|
||||||
|
import { createToast } from "../../Utils/toastUtils";
|
||||||
|
|
||||||
const SetNewPassword = () => {
|
const SetNewPassword = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const [errors, setErrors] = useState({});
|
const [errors, setErrors] = useState({});
|
||||||
const [form, setForm] = useState({});
|
const [form, setForm] = useState({
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
password: "",
|
||||||
|
confirm: "",
|
||||||
|
});
|
||||||
|
|
||||||
const idMap = {
|
const idMap = {
|
||||||
"register-password-input": "password",
|
"register-password-input": "password",
|
||||||
"confirm-password-input": "confirm",
|
"confirm-password-input": "confirm",
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleInput = (e) => {
|
const { isLoading } = useSelector((state) => state.auth);
|
||||||
const fieldName = idMap[e.target.id];
|
const { token } = useParams();
|
||||||
setForm({ ...form, [fieldName]: e.target.value });
|
|
||||||
console.log(errors);
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async (e) => {
|
||||||
// TODO show loading spinner
|
e.preventDefault();
|
||||||
setIsLoading(true);
|
|
||||||
try {
|
|
||||||
await axiosInstance.post("/auth/recovery/validate", {
|
|
||||||
recoveryToken: token,
|
|
||||||
});
|
|
||||||
await axiosInstance.post("/auth/recovery/reset", {
|
|
||||||
...form,
|
|
||||||
recoveryToken: token,
|
|
||||||
});
|
|
||||||
navigate("/new-password-confirmed");
|
|
||||||
} catch (error) {
|
|
||||||
// TODO display error (Toast?)
|
|
||||||
alert(error.response.data.msg);
|
|
||||||
} finally {
|
|
||||||
setIsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
const passwordForm = { ...form };
|
||||||
const { error } = newPasswordValidation.validate(form, {
|
const { error } = credentials.validate(passwordForm, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
|
context: { password: form.password },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
const validationErrors = error.details.reduce((acc, err) => {
|
// validation errors
|
||||||
return { ...acc, [err.path[0]]: err.message };
|
const newErrors = {};
|
||||||
}, {});
|
error.details.forEach((err) => {
|
||||||
setErrors(validationErrors);
|
newErrors[err.path[0]] = err.message;
|
||||||
|
});
|
||||||
|
setErrors(newErrors);
|
||||||
|
createToast({
|
||||||
|
variant: "info",
|
||||||
|
body:
|
||||||
|
error.details && error.details.length > 0
|
||||||
|
? error.details[0].message
|
||||||
|
: "Error validating data.",
|
||||||
|
hasIcon: false,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
setErrors({});
|
delete passwordForm.confirm;
|
||||||
|
const action = await dispatch(
|
||||||
|
setNewPassword({ token: token, form: passwordForm })
|
||||||
|
);
|
||||||
|
if (action.payload.success) {
|
||||||
|
navigate("/new-password-confirmed");
|
||||||
|
createToast({
|
||||||
|
variant: "info",
|
||||||
|
body: "Your password was reset successfully.",
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [form]);
|
};
|
||||||
|
|
||||||
|
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, context: { password: form.password } }
|
||||||
|
);
|
||||||
|
|
||||||
|
setErrors((prev) => {
|
||||||
|
const prevErrors = { ...prev };
|
||||||
|
if (error) prevErrors[name] = error.details[0].message;
|
||||||
|
else delete prevErrors[name];
|
||||||
|
return prevErrors;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const { token } = useParams();
|
|
||||||
return (
|
return (
|
||||||
<div className="set-new-password-page">
|
<div className="set-new-password-page">
|
||||||
<BackgroundPattern />
|
<BackgroundPattern />
|
||||||
<div className="set-new-password-form">
|
<form className="set-new-password-form" onSubmit={handleSubmit}>
|
||||||
<div className="set-new-password-form-header">
|
<div className="set-new-password-form-header">
|
||||||
<img
|
<img
|
||||||
className="set-new-password-form-header-logo"
|
className="set-new-password-form-header-logo"
|
||||||
@@ -89,7 +133,8 @@ const SetNewPassword = () => {
|
|||||||
label="Password"
|
label="Password"
|
||||||
isRequired={true}
|
isRequired={true}
|
||||||
placeholder="••••••••"
|
placeholder="••••••••"
|
||||||
onChange={handleInput}
|
value={form.password}
|
||||||
|
onChange={handleChange}
|
||||||
error={errors.password}
|
error={errors.password}
|
||||||
/>
|
/>
|
||||||
<div className="set-new-password-form-gap-medium"></div>
|
<div className="set-new-password-form-gap-medium"></div>
|
||||||
@@ -99,7 +144,8 @@ const SetNewPassword = () => {
|
|||||||
label="Confirm password"
|
label="Confirm password"
|
||||||
isRequired={true}
|
isRequired={true}
|
||||||
placeholder="••••••••"
|
placeholder="••••••••"
|
||||||
onChange={handleInput}
|
value={form.confirm}
|
||||||
|
onChange={handleChange}
|
||||||
error={errors.confirm}
|
error={errors.confirm}
|
||||||
/>
|
/>
|
||||||
<div className="set-new-password-form-gap-medium"></div>
|
<div className="set-new-password-form-gap-medium"></div>
|
||||||
@@ -109,8 +155,9 @@ const SetNewPassword = () => {
|
|||||||
<Check text="Must contain one special character" />
|
<Check text="Must contain one special character" />
|
||||||
</div>
|
</div>
|
||||||
<div className="set-new-password-form-gap-medium"></div>
|
<div className="set-new-password-form-gap-medium"></div>
|
||||||
<Button
|
<ButtonSpinner
|
||||||
disabled={Object.keys(errors).length !== 0 || isLoading === true}
|
disabled={Object.keys(errors).length !== 0}
|
||||||
|
isLoading={isLoading}
|
||||||
onClick={handleSubmit}
|
onClick={handleSubmit}
|
||||||
level="primary"
|
level="primary"
|
||||||
label="Reset password"
|
label="Reset password"
|
||||||
@@ -133,7 +180,7 @@ const SetNewPassword = () => {
|
|||||||
Back to log in
|
Back to log in
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user