infoalloggi-monorepo/apps/infoalloggi/src/forms/FormRstPwFromToken.tsx

224 lines
6.3 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { Eye, EyeOff } from "lucide-react";
import { useRouter } from "next/router";
import { useState } from "react";
2025-08-04 17:45:44 +02:00
import toast from "react-hot-toast";
2025-08-28 18:27:07 +02:00
import { z } from "zod/v4";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
2025-08-04 17:45:44 +02:00
} from "~/components/custom_ui/form";
import Input from "~/components/ui/input";
2025-08-28 18:27:07 +02:00
import { cn } from "~/lib/utils";
2025-08-04 17:45:44 +02:00
import { useZodForm } from "~/lib/zodForm";
import { useTranslation } from "~/providers/I18nProvider";
2025-08-28 18:27:07 +02:00
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
export const FormRstPwFromToken = (props: { token: string }) => {
2025-08-28 18:27:07 +02:00
const { locale, t } = useTranslation();
const router = useRouter();
const [strengthScore, setStrengthScore] = useState(0);
const [isVisible, setIsVisible] = useState<boolean>(false);
const toggleVisibility = () => setIsVisible((prevState) => !prevState);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
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 },
];
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return requirements.map((req) => ({
met: req.regex.test(pass),
text: req.text,
}));
};
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
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";
};
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const ResetFormSchema = z
.object({
password: z.string(),
repeat_password: z.string(),
})
.superRefine(({ password, repeat_password }, refinementContext) => {
const complexity = checkStrength(password);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (complexity.filter((req) => req.met).length !== complexity.length) {
refinementContext.issues.push({
code: "custom",
2025-08-29 16:18:32 +02:00
input: "",
2025-08-28 18:27:07 +02:00
message: complexity
.filter((req) => !req.met)
.map((req) => req.text)
.join("\n"),
path: ["password"],
});
}
setStrengthScore(complexity.filter((req) => req.met).length);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
if (password !== repeat_password) {
refinementContext.issues.push({
code: "custom",
input: "",
2025-08-29 16:18:32 +02:00
message: t.pwReset.pwDiverse,
path: ["repeat_password"],
2025-08-28 18:27:07 +02:00
});
}
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { mutate } = api.auth.resetPasswordFromToken.useMutation({
2025-08-29 16:18:32 +02:00
onError: (error) => {
toast.error(error.message);
},
2025-08-28 18:27:07 +02:00
onSuccess: async () => {
toast.success(t.pwReset.pwAggiornata);
await router.push("/login");
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(ResetFormSchema, {
defaultValues: {
password: "",
repeat_password: "",
},
});
const onSubmit = (data: z.infer<typeof ResetFormSchema>) => {
mutate({
newpassword: data.password,
resetToken: props.token,
});
};
z.config(z.locales[locale]());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<>
<Form {...form}>
<form
className="space-y-8 px-0.5"
2025-08-29 16:18:32 +02:00
onSubmit={form.handleSubmit(onSubmit)}
2025-08-28 18:27:07 +02:00
>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<div className="flex flex-col gap-x-2">
<FormLabel htmlFor="password">
2025-08-28 18:27:07 +02:00
{t.auth.signup.password}
</FormLabel>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormMessage
className={cn(
"whitespace-pre-wrap",
getStrengthTxt(strengthScore),
)}
/>
</div>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormControl>
<div className="relative">
<Input
{...field}
2025-08-29 16:18:32 +02:00
aria-describedby="password-strength"
aria-invalid={form.getFieldState("password").invalid}
2025-08-28 18:27:07 +02:00
className="pe-9"
2025-08-29 16:18:32 +02:00
id="password"
2025-08-28 18:27:07 +02:00
onChange={async (e) => {
field.onChange(e);
await form.trigger("password");
}}
2025-08-29 16:18:32 +02:00
placeholder="Password"
type={isVisible ? "text" : "password"}
2025-08-28 18:27:07 +02:00
/>
<button
2025-08-29 16:18:32 +02:00
aria-controls="password"
2025-08-28 18:27:07 +02:00
aria-label={isVisible ? "Hide password" : "Show password"}
aria-pressed={isVisible}
className="absolute end-0 top-1 flex size-9 cursor-pointer items-center justify-center rounded-e-lg text-muted-foreground/80 transition-shadow hover:text-foreground focus-visible:border focus-visible:border-ring focus-visible:text-foreground focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring/30 focus-visible:ring-offset-2 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
2025-08-29 16:18:32 +02:00
onClick={toggleVisibility}
type="button"
2025-08-28 18:27:07 +02:00
>
{isVisible ? (
2025-08-29 16:18:32 +02:00
<EyeOff aria-hidden="true" size={16} strokeWidth={2} />
2025-08-28 18:27:07 +02:00
) : (
2025-08-29 16:18:32 +02:00
<Eye aria-hidden="true" size={16} strokeWidth={2} />
2025-08-28 18:27:07 +02:00
)}
</button>
<div
2025-08-29 16:18:32 +02:00
aria-label="Password strength"
aria-valuemax={4}
aria-valuemin={0}
aria-valuenow={strengthScore}
2025-10-10 16:18:43 +02:00
className="mt-3 mb-4 h-1 w-full overflow-hidden rounded-full bg-border"
2025-08-28 18:27:07 +02:00
role="progressbar"
>
<div
className={cn(
"h-full transition-all duration-500 ease-out",
getStrengthColor(strengthScore),
)}
style={{ width: `${(strengthScore / 4) * 100}%` }}
/>
</div>
</div>
</FormControl>
</FormItem>
)}
/>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormField
control={form.control}
name="repeat_password"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel
2025-10-10 16:18:43 +02:00
className="font-medium text-base"
2025-08-28 18:27:07 +02:00
htmlFor="password2"
>
{t.pwReset.ripetiPw}
</FormLabel>
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
<FormMessage />
</div>
<FormControl>
<Input
placeholder=""
{...field}
id="password2"
2025-08-29 16:18:32 +02:00
type="password"
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
<button
className="w-full rounded-lg bg-red-600 px-4 py-2 font-medium text-white duration-150 hover:bg-red-500 active:bg-red-600"
type="submit"
>
{t.pwReset.aggiorna}
</button>
</form>
</Form>
</>
);
2025-08-04 17:45:44 +02:00
};