import toast from "react-hot-toast"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "~/components/custom_ui/form"; import Input from "~/components/custom_ui/input"; import { z } from "zod/v4"; import { useZodForm } from "~/lib/zodForm"; import { api } from "~/utils/api"; import { useTranslation } from "~/providers/I18nProvider"; import { cn } from "~/lib/utils"; import { Eye, EyeOff } from "lucide-react"; import { useRouter } from "next/router"; import { useState } from "react"; export const FormRstPwFromToken = (props: { token: string }) => { const { locale, t } = useTranslation(); const router = useRouter(); const [strengthScore, setStrengthScore] = useState(0); const [isVisible, setIsVisible] = useState(false); const toggleVisibility = () => setIsVisible((prevState) => !prevState); const checkStrength = (pass: string) => { const requirements = [ { regex: /.{8,}/, text: t.auth.signup.pw.lenght }, { regex: /[0-9]/, text: t.auth.signup.pw.number }, { regex: /[a-z]/, text: t.auth.signup.pw.lowercase }, { regex: /[A-Z]/, text: t.auth.signup.pw.uppercase }, ]; return requirements.map((req) => ({ met: req.regex.test(pass), text: req.text, })); }; const getStrengthColor = (score: number) => { if (score === 0) return "bg-border"; if (score <= 1) return "bg-red-500"; if (score <= 2) return "bg-orange-500"; if (score === 3) return "bg-amber-500"; return "bg-emerald-500"; }; const getStrengthTxt = (score: number) => { if (score === 0) return "text-border"; if (score <= 1) return "text-red-500"; if (score <= 2) return "text-orange-500"; if (score === 3) return "text-amber-500"; return "text-emerald-500"; }; const ResetFormSchema = z .object({ password: z.string(), repeat_password: z.string(), }) .superRefine(({ password, repeat_password }, refinementContext) => { const complexity = checkStrength(password); if (complexity.filter((req) => req.met).length !== complexity.length) { refinementContext.issues.push({ code: "custom", message: complexity .filter((req) => !req.met) .map((req) => req.text) .join("\n"), path: ["password"], input: "", }); } setStrengthScore(complexity.filter((req) => req.met).length); if (password !== repeat_password) { refinementContext.issues.push({ message: t.pwReset.pwDiverse, code: "custom", path: ["repeat_password"], input: "", }); } }); const { mutate } = api.auth.resetPasswordFromToken.useMutation({ onSuccess: async () => { toast.success(t.pwReset.pwAggiornata); await router.push("/login"); }, onError: (error) => { toast.error(error.message); }, }); const form = useZodForm(ResetFormSchema, { defaultValues: { password: "", repeat_password: "", }, }); const onSubmit = (data: z.infer) => { mutate({ newpassword: data.password, resetToken: props.token, }); }; z.config(z.locales[locale]()); return ( <>
(
{t.auth.signup.password}
{ field.onChange(e); await form.trigger("password"); }} aria-invalid={form.getFieldState("password").invalid} aria-describedby="password-strength" />
)} /> (
{t.pwReset.ripetiPw}
)} /> ); };