"use client"; import { Eye, EyeOff, Frown } from "lucide-react"; import type { GetServerSideProps } from "next"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; import { z } from "zod"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "~/components/custom_ui/form"; import { PasswordSVG } from "~/components/svgs"; import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { useStrongPassword } from "~/hooks/useStrongPassword"; import { cn } from "~/lib/utils"; import { useZodForm } from "~/lib/zodForm"; import type { NextPageWithLayout } from "~/pages/_app"; import { useTranslation } from "~/providers/I18nProvider"; import { generateSSGHelper } from "~/server/utils/ssgHelper"; import { api } from "~/utils/api"; type Valid = { valid: true; token: string; email: string; }; type Invalid = { valid: false; token: null; email: null; }; type InviteTokenProps = Valid | Invalid; const AcceptInvitePage: NextPageWithLayout = ({ valid, token, email, }: InviteTokenProps) => { const { isVisible, getStrengthColor, passwordRefine, getStrengthTxt, strengthScore, toggleVisibility, } = useStrongPassword(); const router = useRouter(); const schema = z .object({ newPsw: z.string(), }) .superRefine(({ newPsw }, refinementContext) => { passwordRefine({ password: newPsw, path: "newPsw", refinementContext, }); }); type FormValues = z.infer; const { locale } = useTranslation(); z.config(z.locales[locale]()); const form = useZodForm(schema, { defaultValues: { newPsw: "", }, }); function onSubmit(fields: FormValues) { if (!email || !token) return; acceptInvite({ email, password: fields.newPsw, token, }); } const { mutate: acceptInvite, isPending } = api.invite.acceptInvite.useMutation({ onSuccess: async () => { toast.success("Account creato! Effettua il login."); await router.push("/login?redirect=/area-riservata/dashboard"); }, onError: (error) => { toast.error(error.message); }, }); if (!valid) { return (

Questo link non valido

Il link è scaduto o è già stato utilizzato.

Contattaci per ricevere una nuova email.

); } return (

Scegli la tua password

(
Scegli una password
{ field.onChange(e); await form.trigger("newPsw"); }} placeholder="Password" type={isVisible ? "text" : "password"} />
)} />
); }; export default AcceptInvitePage; export const getServerSideProps = (async (context) => { const token = context.params?.token; if (typeof token !== "string") { return { redirect: { destination: "/auth/nonvalid-password-reset-token", permanent: false, }, }; } const helper = generateSSGHelper(); const valid = await helper.invite.verifyInvite.fetch({ token }); if (valid.valid) { return { props: { valid: true, token, email: valid.email as string, }, }; } return { props: { valid: false, token: null, email: null, }, }; }) satisfies GetServerSideProps;