Files
sign/apps/remix/app/components/forms/forgot-password.tsx

95 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-09-18 10:18:33 +00:00
import { zodResolver } from '@hookform/resolvers/zod';
2025-01-02 15:33:37 +11:00
import { msg } from '@lingui/core/macro';
2024-08-27 20:34:39 +09:00
import { useLingui } from '@lingui/react';
2025-01-02 15:33:37 +11:00
import { Trans } from '@lingui/react/macro';
2023-09-18 10:18:33 +00:00
import { useForm } from 'react-hook-form';
2025-01-02 15:33:37 +11:00
import { useNavigate } from 'react-router';
2023-09-18 10:18:33 +00:00
import { z } from 'zod';
2025-01-02 15:33:37 +11:00
import { authClient } from '@documenso/auth/client';
2023-09-18 10:18:33 +00:00
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
2023-11-30 15:55:29 +05:30
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
2023-09-18 10:18:33 +00:00
import { Input } from '@documenso/ui/primitives/input';
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) => {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2023-09-18 11:15:29 +00:00
const { toast } = useToast();
2023-09-18 10:18:33 +00:00
2025-01-02 15:33:37 +11:00
const navigate = useNavigate();
2024-08-27 20:34:39 +09:00
2023-11-30 15:55:29 +05:30
const form = useForm<TForgotPasswordFormSchema>({
2023-09-18 10:18:33 +00:00
values: {
email: '',
},
resolver: zodResolver(ZForgotPasswordFormSchema),
});
2023-11-30 15:55:29 +05:30
const isSubmitting = form.formState.isSubmitting;
2023-09-18 11:15:29 +00:00
const onFormSubmit = async ({ email }: TForgotPasswordFormSchema) => {
2025-01-02 15:33:37 +11:00
await authClient.emailPassword.forgotPassword({ email }).catch(() => null);
await navigate('/check-email');
2023-09-19 13:34:54 +00:00
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Reset email sent`),
description: _(
msg`A password reset email has been sent, if you have an account you should see it in your inbox shortly.`,
),
2023-09-19 13:34:54 +00:00
duration: 5000,
});
2023-11-30 15:55:29 +05:30
form.reset();
2023-09-18 10:18:33 +00:00
};
return (
2023-11-30 15:55:29 +05:30
<Form {...form}>
<form
className={cn('flex w-full flex-col gap-y-4', className)}
onSubmit={form.handleSubmit(onFormSubmit)}
>
<fieldset className="flex w-full flex-col gap-y-4" disabled={isSubmitting}>
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Email</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<Input type="email" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</fieldset>
2023-12-22 01:46:41 +00:00
<Button size="lg" loading={isSubmitting}>
2024-08-27 20:34:39 +09:00
{isSubmitting ? <Trans>Sending Reset Email...</Trans> : <Trans>Reset Password</Trans>}
2023-11-30 15:55:29 +05:30
</Button>
</form>
</Form>
2023-09-18 10:18:33 +00:00
);
};