Files
Checkmate/Client/src/Pages/Auth/SetNewPassword.jsx
T

306 lines
8.8 KiB
React
Raw Normal View History

import { useDispatch, useSelector } from "react-redux";
import { setNewPassword } from "../../Features/Auth/authSlice";
import { createToast } from "../../Utils/toastUtils";
import { Box, Stack, Typography } from "@mui/material";
import { useTheme } from "@emotion/react";
import { useParams } from "react-router-dom";
import { useState } from "react";
import { credentials } from "../../Validation/validation";
2024-06-11 14:18:20 -07:00
import { useNavigate } from "react-router-dom";
import Check from "../../Components/Check/Check";
import Field from "../../Components/Inputs/Field";
2024-09-06 17:14:23 -04:00
import LockIcon from "../../assets/icons/lock.svg?react";
import Background from "../../assets/Images/background-grid.svg?react";
import Logo from "../../assets/icons/bwu-icon.svg?react";
2024-08-31 18:10:50 -04:00
import LoadingButton from "@mui/lab/LoadingButton";
import "./index.css";
2024-09-06 17:14:23 -04:00
import { IconBox } from "./styled";
2024-05-29 22:11:16 +03:30
const SetNewPassword = () => {
2024-06-11 14:18:20 -07:00
const navigate = useNavigate();
const dispatch = useDispatch();
2024-07-16 16:38:27 -04:00
const theme = useTheme();
2024-06-11 14:18:20 -07:00
const [errors, setErrors] = useState({});
const [form, setForm] = useState({
password: "",
confirm: "",
});
2024-06-11 14:18:20 -07:00
const idMap = {
"register-password-input": "password",
"confirm-password-input": "confirm",
};
const { isLoading } = useSelector((state) => state.auth);
const { token } = useParams();
2024-06-11 14:18:20 -07:00
const handleSubmit = async (e) => {
e.preventDefault();
2024-06-11 14:18:20 -07:00
const passwordForm = { ...form };
const { error } = credentials.validate(passwordForm, {
2024-06-11 14:18:20 -07:00
abortEarly: false,
context: { password: form.password },
2024-06-11 14:18:20 -07:00
});
if (error) {
// validation errors
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
createToast({
body:
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data.",
});
2024-06-11 14:18:20 -07:00
} else {
delete passwordForm.confirm;
const action = await dispatch(
setNewPassword({ token: token, form: passwordForm })
);
if (action.payload.success) {
navigate("/new-password-confirmed");
createToast({
body: "Your password was reset successfully.",
});
} else {
if (action.payload) {
// dispatch errors
createToast({
body: action.payload.msg,
});
} else {
// unknown errors
createToast({
body: "Unknown error.",
});
}
}
2024-06-11 14:18:20 -07:00
}
};
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;
});
};
2024-06-11 14:18:20 -07:00
2024-05-29 22:11:16 +03:30
return (
2024-08-27 20:11:59 -04:00
<Stack
className="set-new-password-page auth"
overflow="hidden"
sx={{
"& h1": {
color: theme.palette.primary.main,
2024-08-27 20:11:59 -04:00
fontWeight: 600,
fontSize: 24,
},
"& p": {
fontSize: 14,
color: theme.palette.text.accent,
},
}}
>
<Box
2024-07-17 17:44:29 -04:00
className="background-pattern-svg"
2024-09-06 17:14:23 -04:00
sx={{
"& svg g g:last-of-type path": {
stroke: theme.palette.border.light,
},
}}
>
<Background style={{ width: "100%" }} />
</Box>
<Stack
direction="row"
alignItems="center"
2024-08-27 20:11:59 -04:00
px={theme.spacing(12)}
gap={theme.spacing(4)}
>
<Logo style={{ borderRadius: theme.shape.borderRadius }} />
<Typography sx={{ userSelect: "none" }}>BlueWave Uptime</Typography>
</Stack>
<Stack
width="100%"
maxWidth={600}
flex={1}
justifyContent="center"
2024-08-27 20:11:59 -04:00
px={{ xs: theme.spacing(12), lg: theme.spacing(20) }}
pb={theme.spacing(12)}
mx="auto"
sx={{
"& > .MuiStack-root": {
border: 1,
2024-08-27 20:11:59 -04:00
borderRadius: theme.spacing(5),
borderColor: theme.palette.border.light,
backgroundColor: theme.palette.background.main,
padding: {
2024-08-27 20:11:59 -04:00
xs: theme.spacing(12),
sm: theme.spacing(20),
},
},
}}
2024-08-12 16:26:42 -04:00
>
<Stack
2024-08-27 20:11:59 -04:00
gap={{ xs: theme.spacing(8), sm: theme.spacing(12) }}
alignItems="center"
textAlign="center"
>
2024-08-12 16:26:42 -04:00
<Box>
2024-09-06 17:14:23 -04:00
<IconBox>
<LockIcon alt="lock icon" />
</IconBox>
2024-08-12 16:26:42 -04:00
<Typography component="h1">Set new password</Typography>
<Typography>
Your new password must be different to previously used passwords.
</Typography>
</Box>
<Box
width="100%"
textAlign="left"
sx={{
"& .input-error": {
display: "none",
},
}}
>
<Box
component="form"
noValidate
spellCheck={false}
onSubmit={handleSubmit}
>
2024-08-12 16:26:42 -04:00
<Field
type="password"
id="register-password-input"
label="Password"
isRequired={true}
placeholder="••••••••"
value={form.password}
onChange={handleChange}
error={errors.password}
/>
</Box>
<Box
component="form"
noValidate
spellCheck={false}
onSubmit={handleSubmit}
>
2024-08-12 16:26:42 -04:00
<Field
type="password"
id="confirm-password-input"
label="Confirm password"
isRequired={true}
placeholder="••••••••"
value={form.confirm}
onChange={handleChange}
error={errors.confirm}
/>
</Box>
2024-08-27 20:11:59 -04:00
<Stack gap={theme.spacing(4)} mb={theme.spacing(12)}>
2024-08-12 16:26:42 -04:00
<Check
text={
<>
<Typography component="span">Must be at least</Typography> 8
characters long
</>
}
2024-08-12 16:26:42 -04:00
variant={
errors?.password === "Password is required"
? "error"
: form.password === ""
? "info"
: form.password.length < 8
? "error"
: "success"
}
/>
<Check
text={
<>
<Typography component="span">Must contain</Typography> one
special character and a number
</>
}
2024-08-12 16:26:42 -04:00
variant={
errors?.password === "Password is required"
? "error"
: form.password === ""
? "info"
: !/^(?=.*[!@#$%^&*(),.?":{}|])(?=.*\d).+$/.test(
form.password
)
? "error"
: "success"
}
/>
<Check
text={
<>
<Typography component="span">
Must contain at least
</Typography>{" "}
one upper and lower character
</>
}
2024-08-12 16:26:42 -04:00
variant={
errors?.password === "Password is required"
? "error"
: form.password === ""
? "info"
: !/^(?=.*[A-Z])(?=.*[a-z]).+$/.test(form.password)
? "error"
: "success"
}
/>
</Stack>
</Box>
2024-08-31 18:10:50 -04:00
<LoadingButton
variant="contained"
color="primary"
loading={isLoading}
2024-08-12 16:26:42 -04:00
onClick={handleSubmit}
2024-08-31 18:10:50 -04:00
disabled={Object.keys(errors).length !== 0}
sx={{ width: "100%", maxWidth: 400 }}
2024-08-31 18:10:50 -04:00
>
Reset password
</LoadingButton>
2024-08-12 16:26:42 -04:00
</Stack>
</Stack>
2024-08-27 20:11:59 -04:00
<Box textAlign="center" p={theme.spacing(12)}>
<Typography display="inline-block">Go back to —</Typography>
<Typography
component="span"
color={theme.palette.primary.main}
2024-08-27 20:11:59 -04:00
ml={theme.spacing(2)}
onClick={() => navigate("/login")}
sx={{ userSelect: "none" }}
>
Log In
</Typography>
</Box>
</Stack>
2024-05-29 22:11:16 +03:30
);
};
export default SetNewPassword;