2023-09-18 10:31:33 +00:00
|
|
|
'use client';
|
|
|
|
|
|
2023-09-18 10:33:03 +00:00
|
|
|
import { useRouter } from 'next/navigation';
|
2023-09-18 10:31: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 14:03:33 +00:00
|
|
|
import { trpc } from '@documenso/trpc/react';
|
2023-09-18 10:31: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 14:03:33 +00:00
|
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
2023-09-18 10:31:33 +00:00
|
|
|
|
|
|
|
|
export const ZResetPasswordFormSchema = z
|
|
|
|
|
.object({
|
|
|
|
|
password: z.string().min(6).max(72),
|
|
|
|
|
repeatedPassword: z.string().min(6).max(72),
|
|
|
|
|
})
|
|
|
|
|
.refine((data) => data.password === data.repeatedPassword, {
|
|
|
|
|
path: ['repeatedPassword'],
|
|
|
|
|
message: "Password don't match",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type TResetPasswordFormSchema = z.infer<typeof ZResetPasswordFormSchema>;
|
|
|
|
|
|
|
|
|
|
export type ResetPasswordFormProps = {
|
|
|
|
|
className?: string;
|
2023-09-18 14:03:33 +00:00
|
|
|
token: string;
|
2023-09-18 10:31:33 +00:00
|
|
|
};
|
|
|
|
|
|
2023-09-18 14:03:33 +00:00
|
|
|
export const ResetPasswordForm = ({ className, token }: ResetPasswordFormProps) => {
|
2023-09-18 10:31:33 +00:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
2023-09-18 14:03:33 +00:00
|
|
|
const { toast } = useToast();
|
|
|
|
|
|
2023-09-18 10:31:33 +00:00
|
|
|
const {
|
|
|
|
|
register,
|
2023-09-18 14:03:33 +00:00
|
|
|
reset,
|
2023-09-18 10:31:33 +00:00
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
|
} = useForm<TResetPasswordFormSchema>({
|
|
|
|
|
values: {
|
|
|
|
|
password: '',
|
|
|
|
|
repeatedPassword: '',
|
|
|
|
|
},
|
|
|
|
|
resolver: zodResolver(ZResetPasswordFormSchema),
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-18 14:03:33 +00:00
|
|
|
const { mutateAsync: resetPassword } = trpc.profile.resetPassword.useMutation();
|
|
|
|
|
|
|
|
|
|
const onFormSubmit = async ({ password, repeatedPassword }: TResetPasswordFormSchema) => {
|
|
|
|
|
// TODO: Handle error with try/catch
|
|
|
|
|
console.log(password, repeatedPassword, token);
|
|
|
|
|
|
|
|
|
|
await resetPassword({
|
|
|
|
|
password,
|
|
|
|
|
token,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reset();
|
|
|
|
|
|
|
|
|
|
toast({
|
|
|
|
|
title: 'Password updated',
|
|
|
|
|
description: 'Your password has been updated successfully.',
|
|
|
|
|
duration: 5000,
|
|
|
|
|
});
|
2023-09-18 10:31:33 +00:00
|
|
|
|
|
|
|
|
router.push('/signin');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
className={cn('flex w-full flex-col gap-y-4', className)}
|
|
|
|
|
onSubmit={handleSubmit(onFormSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="password" className="flex justify-between text-slate-500">
|
|
|
|
|
<span>Password</span>
|
|
|
|
|
</Label>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
id="password"
|
|
|
|
|
type="password"
|
|
|
|
|
minLength={6}
|
|
|
|
|
maxLength={72}
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
className="bg-background mt-2"
|
|
|
|
|
{...register('password')}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{errors.password && (
|
|
|
|
|
<span className="mt-1 text-xs text-red-500">{errors.password.message}</span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div>
|
|
|
|
|
<Label htmlFor="repeatedPassword" className="flex justify-between text-slate-500">
|
2023-09-18 10:33:03 +00:00
|
|
|
<span>Repeat Password</span>
|
2023-09-18 10:31:33 +00:00
|
|
|
</Label>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
id="repeatedPassword"
|
|
|
|
|
type="password"
|
|
|
|
|
minLength={6}
|
|
|
|
|
maxLength={72}
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
className="bg-background mt-2"
|
|
|
|
|
{...register('repeatedPassword')}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
{errors.repeatedPassword && (
|
|
|
|
|
<span className="mt-1 text-xs text-red-500">{errors.repeatedPassword.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>
|
|
|
|
|
);
|
|
|
|
|
};
|