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

233 lines
6.7 KiB
TypeScript
Raw Normal View History

2025-08-28 18:27:07 +02:00
import { Eye, EyeOff } from "lucide-react";
import { useState } from "react";
import toast from "react-hot-toast";
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/custom_ui/input";
import { Button, buttonVariants } from "~/components/ui/button";
import {
2025-08-28 18:27:07 +02:00
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
2025-08-04 17:45:44 +02:00
} from "~/components/ui/dialog";
import { Label } from "~/components/ui/label";
2025-08-28 18:27:07 +02:00
import { Separator } from "~/components/ui/separator";
import { useStrongPassword } from "~/hooks/useStrongPassword";
2025-08-04 17:45:44 +02:00
import { generatePassword } from "~/lib/basicPwGenerator";
import { cn } from "~/lib/utils";
2025-08-28 18:27:07 +02:00
import { useZodForm } from "~/lib/zodForm";
import { useTranslation } from "~/providers/I18nProvider";
import type { UsersId } from "~/schemas/public/Users";
import { api } from "~/utils/api";
2025-08-04 17:45:44 +02:00
export const OverridePasswordForm = ({ id }: { id: UsersId }) => {
2025-08-28 18:27:07 +02:00
const {
isVisible,
getStrengthColor,
passwordRefine,
getStrengthTxt,
strengthScore,
toggleVisibility,
} = useStrongPassword();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const OverridePasswordFormSchema = z
.object({
password: z.string(),
})
.superRefine(({ password }, refinementContext) => {
passwordRefine({
password,
path: "password",
2025-08-29 16:18:32 +02:00
refinementContext,
2025-08-28 18:27:07 +02:00
});
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
type OverridePasswordFormValues = z.infer<typeof OverridePasswordFormSchema>;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { locale, t } = useTranslation();
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
z.config(z.locales[locale]());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(OverridePasswordFormSchema, {
defaultValues: {
password: "",
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const { mutate } = api.auth.OverridePassword.useMutation({
2025-08-29 16:18:32 +02:00
onError: (error) => {
toast.error(error.message);
},
2025-08-28 18:27:07 +02:00
onSuccess: () => {
toast.success(t.overridePw.pwAggiornata);
form.reset();
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
async function onSubmit(fields: OverridePasswordFormValues) {
mutate({
id: id,
password: fields.password,
});
form.reset();
await form.trigger("password");
}
const [resetToken, setResetToken] = useState("");
const { mutate: resetToken_mutate } = api.auth.genPswResetToken.useMutation({
onSuccess: (data) => {
setResetToken(data);
},
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<Form {...form}>
2025-08-29 16:18:32 +02:00
<form className="space-y-8 px-0.5" onSubmit={form.handleSubmit(onSubmit)}>
2025-08-28 18:27:07 +02:00
<div className="flex flex-col items-start gap-2">
<Label htmlFor="resetLink">Genera un Link di Reset Password</Label>
<Dialog>
<DialogTrigger asChild>
2025-08-29 16:18:32 +02:00
<Button id="resetLink" variant="info">
2025-08-28 18:27:07 +02:00
Pw Reset link
</Button>
</DialogTrigger>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Strumento Reset Password</DialogTitle>
<DialogDescription className="sr-only">Edit</DialogDescription>
</DialogHeader>
<div className="space-y-4 py-4">
<Button
2025-10-10 16:18:43 +02:00
className="inline-flex h-10 items-center justify-center whitespace-nowrap rounded-md bg-blue-500 px-4 py-2 font-medium text-sm text-white ring-offset-white transition-colors hover:bg-blue-500/90"
2025-08-28 18:27:07 +02:00
onClick={() => resetToken_mutate({ id })}
>
Genera
</Button>
<Separator />
<h4>Reset Token</h4>
<Button
onClick={async () => {
await navigator.clipboard.writeText(resetToken);
toast("Token copiato", { icon: "🔑" });
}}
2025-08-29 16:18:32 +02:00
variant="link"
2025-08-28 18:27:07 +02:00
>
{resetToken}
</Button>
<Separator />
<h4>Link</h4>
<Button
onClick={async () => {
await navigator.clipboard.writeText(
`infoalloggi.it/auth/password-reset/${resetToken}`,
);
toast("Link copiato", { icon: "🔗" });
}}
2025-08-29 16:18:32 +02:00
variant="link"
2025-08-28 18:27:07 +02:00
>
{`infoalloggi.it/auth/password-reset/${resetToken}`}
</Button>
</div>
</DialogContent>
</Dialog>
</div>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
<div className="flex flex-col gap-x-2">
<div className="flex items-center justify-between">
<FormLabel htmlFor="password">
{t.auth.signup.password}
</FormLabel>{" "}
<button
className={cn(
buttonVariants({
size: "sm",
2025-08-29 16:18:32 +02:00
variant: "outline",
2025-08-28 18:27:07 +02:00
}),
)}
onClick={async () => {
form.setValue("password", generatePassword());
await form.trigger("password");
}}
2025-08-29 16:18:32 +02:00
type="button"
2025-08-28 18:27:07 +02:00
>
genera password
</button>
</div>
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}
2025-10-10 16:18:43 +02:00
className="absolute end-0 top-1 flex size-9 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>
)}
/>
<Button type="submit">{t.overridePw.aggiorna}</Button>
</form>
</Form>
);
2025-08-04 17:45:44 +02:00
};