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

161 lines
4.7 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10: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-06-09 18:21:18 +10:00
import { useForm } from 'react-hook-form';
2024-12-06 16:01:24 +09:00
import { match } from 'ts-pattern';
2023-06-09 18:21:18 +10:00
import { z } from 'zod';
2025-01-02 15:33:37 +11:00
import { authClient } from '@documenso/auth/client';
import type { SessionUser } from '@documenso/auth/server/lib/session/session';
2024-12-06 16:01:24 +09:00
import { AppError } from '@documenso/lib/errors/app-error';
import { ZCurrentPasswordSchema, ZPasswordSchema } from '@documenso/trpc/server/auth-router/schema';
2023-06-09 18:21:18 +10:00
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
2023-11-29 22:32:42 +05:30
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@documenso/ui/primitives/form/form';
import { PasswordInput } from '@documenso/ui/primitives/password-input';
2023-06-09 18:21:18 +10:00
import { useToast } from '@documenso/ui/primitives/use-toast';
export const ZPasswordFormSchema = z
.object({
currentPassword: ZCurrentPasswordSchema,
password: ZPasswordSchema,
repeatedPassword: ZPasswordSchema,
2023-06-09 18:21:18 +10:00
})
.refine((data) => data.password === data.repeatedPassword, {
message: 'Passwords do not match',
path: ['repeatedPassword'],
});
export type TPasswordFormSchema = z.infer<typeof ZPasswordFormSchema>;
export type PasswordFormProps = {
className?: string;
2025-01-02 15:33:37 +11:00
user: SessionUser;
2023-06-09 18:21:18 +10:00
};
export const PasswordForm = ({ className }: PasswordFormProps) => {
2024-08-27 20:34:39 +09:00
const { _ } = useLingui();
2023-06-09 18:21:18 +10:00
const { toast } = useToast();
2023-11-29 22:32:42 +05:30
const form = useForm<TPasswordFormSchema>({
2023-06-09 18:21:18 +10:00
values: {
currentPassword: '',
2023-06-09 18:21:18 +10:00
password: '',
repeatedPassword: '',
},
resolver: zodResolver(ZPasswordFormSchema),
});
2023-11-29 22:32:42 +05:30
const isSubmitting = form.formState.isSubmitting;
const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => {
2023-06-09 18:21:18 +10:00
try {
2025-01-02 15:33:37 +11:00
await authClient.emailPassword.updatePassword({
currentPassword,
2023-06-09 18:21:18 +10:00
password,
});
2023-11-29 22:32:42 +05:30
form.reset();
2023-08-30 03:09:40 +00:00
2023-06-09 18:21:18 +10:00
toast({
2024-08-27 20:34:39 +09:00
title: _(msg`Password updated`),
description: _(msg`Your password has been updated successfully.`),
2023-06-09 18:21:18 +10:00
duration: 5000,
});
} catch (err) {
2024-12-06 16:01:24 +09:00
const error = AppError.parseError(err);
const errorMessage = match(error.code)
.with('NO_PASSWORD', () => msg`User has no password.`)
.with('INCORRECT_PASSWORD', () => msg`Current password is incorrect.`)
.with(
'SAME_PASSWORD',
() => msg`Your new password cannot be the same as your old password.`,
)
.otherwise(
() =>
2024-08-27 20:34:39 +09:00
msg`We encountered an unknown error while attempting to update your password. Please try again later.`,
2024-12-06 16:01:24 +09:00
);
toast({
title: _(msg`An error occurred`),
description: _(errorMessage),
variant: 'destructive',
});
2023-06-09 18:21:18 +10:00
}
};
return (
2023-11-29 22:32:42 +05:30
<Form {...form}>
<form
className={cn('flex w-full flex-col gap-y-4', className)}
onSubmit={form.handleSubmit(onFormSubmit)}
>
2023-11-30 15:55:29 +05:30
<fieldset className="flex w-full flex-col gap-y-4" disabled={isSubmitting}>
<FormField
control={form.control}
name="currentPassword"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Current Password</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<PasswordInput autoComplete="current-password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="password"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Password</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<PasswordInput autoComplete="new-password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="repeatedPassword"
render={({ field }) => (
<FormItem>
2024-08-27 20:34:39 +09:00
<FormLabel>
<Trans>Repeat Password</Trans>
</FormLabel>
2023-11-30 15:55:29 +05:30
<FormControl>
<PasswordInput autoComplete="new-password" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</fieldset>
2023-11-29 22:32:42 +05:30
2024-01-30 17:31:27 +11:00
<div className="ml-auto mt-4">
2023-12-22 01:46:41 +00:00
<Button type="submit" loading={isSubmitting}>
2024-08-27 20:34:39 +09:00
{isSubmitting ? <Trans>Updating password...</Trans> : <Trans>Update password</Trans>}
2023-09-20 02:18:35 +00:00
</Button>
</div>
2023-11-29 22:32:42 +05:30
</form>
</Form>
2023-06-09 18:21:18 +10:00
);
};