Files
Checkmate/client/src/Pages/Auth/SetNewPassword/index.tsx
T

138 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-02-20 18:13:39 +00:00
import { BaseAuthPage, BulletPointCheck, TextLink } from "@/Components/design-elements";
import { Button, TextField } from "@/Components/inputs";
2026-02-09 21:29:55 +00:00
import Stack from "@mui/material/Stack";
import { useTheme } from "@mui/material/styles";
2026-02-09 21:21:17 +00:00
import { useTranslation } from "react-i18next";
2026-02-09 21:56:28 +00:00
import { useForm, Controller } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
import { useSetNewPasswordForm } from "@/Hooks/useSetNewPasswordForm";
import type { SetNewPasswordFormData } from "@/Validation/setNewPassword";
import { specialCharPattern } from "@/Validation/patterns";
import { useParams, useNavigate } from "react-router-dom";
import { usePost } from "@/Hooks/UseApi";
import { useDispatch } from "react-redux";
import { setAuthState } from "@/Features/Auth/authSlice";
2026-02-09 21:00:27 +00:00
const SetNewPasswordPage = () => {
2026-02-09 21:29:55 +00:00
const theme = useTheme();
2026-02-09 21:21:17 +00:00
const { t } = useTranslation();
2026-02-09 21:56:28 +00:00
const { token } = useParams<{ token: string }>();
const navigate = useNavigate();
const dispatch = useDispatch();
const { post, loading } = usePost();
const { schema, defaults } = useSetNewPasswordForm();
const { control, handleSubmit, watch } = useForm<SetNewPasswordFormData>({
resolver: zodResolver(schema),
defaultValues: defaults,
});
const password = watch("password");
const confirm = watch("confirm");
const getVariant = (condition: boolean): "success" | "info" => {
return condition ? "success" : "info";
};
const hasLength = password.length >= 8;
const hasSpecial = specialCharPattern.test(password);
const hasNumber = /\d/.test(password);
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const passwordsMatch = password.length > 0 && password === confirm;
const onSubmit = async (data: SetNewPasswordFormData) => {
if (loading) return;
const result = await post("/auth/recovery/reset", {
password: data.password,
recoveryToken: token,
});
if (result?.success) {
dispatch(setAuthState(result));
navigate("/uptime");
}
};
2026-02-09 21:29:55 +00:00
2026-02-09 21:00:27 +00:00
return (
<BaseAuthPage
2026-02-09 21:56:28 +00:00
component="form"
onSubmit={handleSubmit(onSubmit)}
2026-02-09 21:21:17 +00:00
title={t("pages.auth.setNewPassword.title")}
subtitle={t("pages.auth.setNewPassword.subtitle")}
2026-02-09 21:00:27 +00:00
>
2026-02-09 21:56:28 +00:00
<Controller
name="password"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t("pages.auth.common.form.option.password.label")}
placeholder={t("pages.auth.common.form.option.password.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
2026-02-09 21:21:17 +00:00
/>
2026-02-09 21:56:28 +00:00
<Controller
name="confirm"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t("pages.auth.common.form.option.confirmPassword.label")}
placeholder={t("pages.auth.common.form.option.password.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error?.message ?? ""}
/>
)}
2026-02-09 21:21:17 +00:00
/>
2026-02-09 21:29:55 +00:00
<Stack gap={theme.spacing(4)}>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.length")}
2026-02-09 21:56:28 +00:00
variant={getVariant(hasLength)}
2026-02-09 21:29:55 +00:00
/>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.special")}
2026-02-09 21:56:28 +00:00
variant={getVariant(hasSpecial)}
2026-02-09 21:29:55 +00:00
/>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.number")}
2026-02-09 21:56:28 +00:00
variant={getVariant(hasNumber)}
2026-02-09 21:29:55 +00:00
/>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.uppercase")}
2026-02-09 21:56:28 +00:00
variant={getVariant(hasUppercase)}
2026-02-09 21:29:55 +00:00
/>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.lowercase")}
2026-02-09 21:56:28 +00:00
variant={getVariant(hasLowercase)}
2026-02-09 21:29:55 +00:00
/>
<BulletPointCheck
2026-02-13 20:42:26 +00:00
text={t("pages.auth.common.passwordRules.match")}
2026-02-09 21:56:28 +00:00
variant={getVariant(passwordsMatch)}
2026-02-09 21:29:55 +00:00
/>
</Stack>
2026-02-09 21:21:17 +00:00
<Button
variant="contained"
type="submit"
fullWidth
2026-02-09 21:56:28 +00:00
loading={loading}
2026-02-09 21:21:17 +00:00
>
2026-02-13 20:42:26 +00:00
{t("common.buttons.resetPassword")}
2026-02-09 21:21:17 +00:00
</Button>
<TextLink
alignSelf="center"
text={t("pages.auth.forgotPassword.links.login.text")}
linkText={t("pages.auth.forgotPassword.links.login.linkText")}
href="/login"
/>
2026-02-09 21:00:27 +00:00
</BaseAuthPage>
);
};
export default SetNewPasswordPage;