import { useRouter } from "next/navigation"; import { zodResolver } from "@hookform/resolvers/zod"; import { t } from "@lingui/core/macro"; import { useMutation } from "@tanstack/react-query"; import { env } from "next-runtime-env"; import { useForm } from "react-hook-form"; import { z } from "zod"; import { authClient } from "@kan/auth/client"; import Button from "~/components/Button"; import Input from "~/components/Input"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; const FormSchema = z .object({ currentPassword: z.string().min(1, t`Current password is required`), newPassword: z .string() .min(8, t`Password must be at least 8 characters`) .min(1, t`New password is required`), confirmPassword: z.string().min(1, t`Please confirm your new password`), }) .refine((data) => data.newPassword === data.confirmPassword, { message: t`Passwords do not match`, path: ["confirmPassword"], }) .refine((data) => data.currentPassword !== data.newPassword, { message: t`New password must be different from current password`, path: ["newPassword"], }); type FormValues = z.infer; export function ChangePasswordFormConfirmation() { const { closeModal } = useModal(); const { showPopup } = usePopup(); const router = useRouter(); const utils = api.useUtils(); const { register, handleSubmit, formState: { errors, isValid }, reset, setError, } = useForm({ resolver: zodResolver(FormSchema), mode: "onChange", }); const changePasswordMutation = useMutation({ mutationFn: async (data: FormValues) => { const response = await authClient.changePassword({ newPassword: data.newPassword, currentPassword: data.currentPassword, revokeOtherSessions: true, }); if (response?.error) { throw new Error(response?.error.message || "Invalid Password"); } }, onSuccess: async () => { closeModal(); showPopup({ header: t`Password Changed`, message: t`Your password has been changed.`, icon: "success", }); utils.invalidate(); reset(); router.push("/"); }, onError: async (error: any) => { const errorMessage = error.message.toLowerCase(); if (errorMessage.includes("invalid password")) { setError("currentPassword", { type: "manual", message: t`The current password you entered is incorrect.`, }); } else { closeModal(); showPopup({ header: t`Error Changing Password`, message: t`An unexpected error occurred. Please try again later.`, icon: "error", }); } }, }); const onSubmit = (data: FormValues) => { changePasswordMutation.mutate(data); }; const handleCancel = () => { reset(); closeModal(); }; return (

{t`Change Password`}

{t`Enter your current password and choose a new secure password.`}

{errors.currentPassword && (

{errors.currentPassword.message}

)}
{errors.newPassword && (

{errors.newPassword.message}

)}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
); }