From 78d2401d444c788f338ae0ccf71af5dec8e7d439 Mon Sep 17 00:00:00 2001 From: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Date: Wed, 4 Oct 2023 09:53:57 +0530 Subject: [PATCH] feat: require old password for password reset (#488) * feat: require old password for password reset --- apps/web/src/components/forms/password.tsx | 39 ++++++++++++++++++- .../lib/server-only/user/update-password.ts | 33 ++++++++++------ packages/trpc/server/profile-router/router.ts | 3 +- packages/trpc/server/profile-router/schema.ts | 1 + 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/apps/web/src/components/forms/password.tsx b/apps/web/src/components/forms/password.tsx index 8b6a58a06..5df5005f1 100644 --- a/apps/web/src/components/forms/password.tsx +++ b/apps/web/src/components/forms/password.tsx @@ -20,6 +20,7 @@ import { FormErrorMessage } from '../form/form-error-message'; export const ZPasswordFormSchema = z .object({ + currentPassword: z.string().min(6).max(72), password: z.string().min(6).max(72), repeatedPassword: z.string().min(6).max(72), }) @@ -40,6 +41,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => { const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); + const [showCurrentPassword, setShowCurrentPassword] = useState(false); const { register, @@ -48,6 +50,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => { formState: { errors, isSubmitting }, } = useForm({ values: { + currentPassword: '', password: '', repeatedPassword: '', }, @@ -56,9 +59,10 @@ export const PasswordForm = ({ className }: PasswordFormProps) => { const { mutateAsync: updatePassword } = trpc.profile.updatePassword.useMutation(); - const onFormSubmit = async ({ password }: TPasswordFormSchema) => { + const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => { try { await updatePassword({ + currentPassword, password, }); @@ -92,6 +96,39 @@ export const PasswordForm = ({ className }: PasswordFormProps) => { className={cn('flex w-full flex-col gap-y-4', className)} onSubmit={handleSubmit(onFormSubmit)} > +
+ + +
+ + + +
+ + +