2023-09-18 10:18:33 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2023-09-18 10:33:03 +00:00
|
|
|
import { useRouter } from 'next/navigation';
|
2023-09-18 10:18:33 +00:00
|
|
|
|
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
|
|
import { Loader } from 'lucide-react';
|
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
|
2023-09-18 15:09:41 +00:00
|
|
|
import { TRPCClientError } from '@documenso/trpc/client';
|
2023-09-18 11:15:29 +00:00
|
|
|
import { trpc } from '@documenso/trpc/react';
|
2023-09-18 10:18:33 +00:00
|
|
|
import { cn } from '@documenso/ui/lib/utils';
|
|
|
|
|
import { Button } from '@documenso/ui/primitives/button';
|
|
|
|
|
import { Input } from '@documenso/ui/primitives/input';
|
|
|
|
|
import { Label } from '@documenso/ui/primitives/label';
|
2023-09-18 11:15:29 +00:00
|
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
2023-09-18 10:18:33 +00:00
|
|
|
|
|
|
|
|
export const ZForgotPasswordFormSchema = z.object({
|
|
|
|
|
email: z.string().email().min(1),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type TForgotPasswordFormSchema = z.infer<typeof ZForgotPasswordFormSchema>;
|
|
|
|
|
|
|
|
|
|
export type ForgotPasswordFormProps = {
|
|
|
|
|
className?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const ForgotPasswordForm = ({ className }: ForgotPasswordFormProps) => {
|
|
|
|
|
const router = useRouter();
|
2023-09-18 11:15:29 +00:00
|
|
|
const { toast } = useToast();
|
2023-09-18 10:18:33 +00:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
2023-09-18 11:15:29 +00:00
|
|
|
reset,
|
2023-09-18 10:18:33 +00:00
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
|
} = useForm<TForgotPasswordFormSchema>({
|
|
|
|
|
values: {
|
|
|
|
|
email: '',
|
|
|
|
|
},
|
|
|
|
|
resolver: zodResolver(ZForgotPasswordFormSchema),
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-18 11:15:29 +00:00
|
|
|
const { mutateAsync: forgotPassword } = trpc.profile.forgotPassword.useMutation();
|
|
|
|
|
|
|
|
|
|
const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => {
|
2023-09-18 15:09:41 +00:00
|
|
|
try {
|
|
|
|
|
await forgotPassword({ email });
|
|
|
|
|
|
|
|
|
|
toast({
|
|
|
|
|
title: 'Reset email sent',
|
|
|
|
|
description: 'Your password reset mail has been sent successfully.',
|
|
|
|
|
duration: 5000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
router.push('/check-email');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') {
|
|
|
|
|
toast({
|
|
|
|
|
title: 'An error occurred',
|
|
|
|
|
description: err.message,
|
|
|
|
|
variant: 'destructive',
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
toast({
|
|
|
|
|
title: 'An unknown error occurred',
|
|
|
|
|
variant: 'destructive',
|
|
|
|
|
description:
|
|
|
|
|
'We encountered an unknown error while attempting to send your email. Please try again later.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-18 10:18:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
className={cn('flex w-full flex-col gap-y-4', className)}
|
|
|
|
|
onSubmit={handleSubmit(onFormSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="email" className="text-slate-500">
|
|
|
|
|
Email
|
|
|
|
|
</Label>
|
|
|
|
|
|
|
|
|
|
<Input id="email" type="email" className="bg-background mt-2" {...register('email')} />
|
|
|
|
|
|
|
|
|
|
{errors.email && <span className="mt-1 text-xs text-red-500">{errors.email.message}</span>}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button size="lg" disabled={isSubmitting} className="dark:bg-documenso dark:hover:opacity-90">
|
|
|
|
|
{isSubmitting && <Loader className="mr-2 h-5 w-5 animate-spin" />}
|
|
|
|
|
Reset Password
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
};
|