import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { Button } from "@documenso/ui"; import Logo from "./logo"; import { ArrowLeftIcon } from "@heroicons/react/24/outline"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { toast } from "react-hot-toast"; import * as z from "zod"; const ZResetPasswordFormSchema = z .object({ password: z.string().min(8, { message: "Password must be at least 8 characters" }), confirmPassword: z.string().min(8, { message: "Password must be at least 8 characters" }), }) .refine((data) => data.password === data.confirmPassword, { path: ["confirmPassword"], message: "Password don't match", }); type TResetPasswordFormSchema = z.infer; export default function ResetPassword() { const router = useRouter(); const { token } = router.query; const { register, formState: { errors, isSubmitting }, handleSubmit, } = useForm({ resolver: zodResolver(ZResetPasswordFormSchema), }); const [resetSuccessful, setResetSuccessful] = useState(false); const onSubmit = async ({ password }: TResetPasswordFormSchema) => { const response = await toast.promise( fetch(`/api/auth/reset-password`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ password, token }), }), { loading: "Resetting...", success: `Reset password successful`, error: "Could not reset password :/", } ); if (!response.ok) { toast.dismiss(); const error = await response.json(); toast.error(error.message); } if (response.ok) { setResetSuccessful(true); setTimeout(() => { router.push("/login"); }, 3000); } }; return ( <>

Reset Password

{resetSuccessful ? "Your password has been reset." : "Please chose your new password"}

{!resetSuccessful && (
{errors && ( {errors.confirmPassword?.message} )}
)}
Back to log in
); }