diff --git a/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/password.tsx b/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/password.tsx index c818f5fa5..4b5a99a26 100644 --- a/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/password.tsx +++ b/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/password.tsx @@ -1,64 +1,52 @@ "use client"; +import { zodResolver } from "@hookform/resolvers/zod"; import axios from "axios"; import { useSession } from "next-auth/react"; -import { ChangeEvent, useState } from "react"; +import { useState } from "react"; +import { useForm } from "react-hook-form"; import { getApiUrl } from "~/actions/getApiUrl"; import CustomButton from "~/components/common/common-button/common-button"; -import CustomInput from "~/components/common/input/input"; import PasswordSuccessfulModal from "~/components/common/modals/password-successful"; import { toast } from "~/components/ui/use-toast"; +import { cn } from "~/lib/utils"; +import { passwordSchema, type PasswordFormData } from "./schema"; const Password = () => { const { data } = useSession(); const [open, setOpen] = useState(false); - const [isPending, setIsPending] = useState(false); - const [formData, setFormData] = useState({ - oldPassword: "", - password: "", - confirmPassword: "", + const { + register, + handleSubmit, + reset, + formState: { errors, isValid }, + } = useForm({ + resolver: zodResolver(passwordSchema), + mode: "all", }); - const formDataHandler = ( - event: ChangeEvent, - ) => { - setFormData((previous) => ({ - ...previous, - [event.target.name]: event.target.value, - })); - }; - const submit = async () => { - if (formData.password !== formData.confirmPassword) { - return toast({ - title: "Warning!", - description: "Password does not match", - }); - } + const submitHandler = async (values: PasswordFormData) => { try { setIsPending(true); - const baseUrl = await getApiUrl(); - const API_URL = `${baseUrl}/api/v1/auth/change-password`; - const payload = { - oldPassword: formData.oldPassword, - newPassword: formData.password, + old_password: values.currentPassword, + new_password: values.newPassword, + confirm_new_password: values.confirmPassword, }; + const baseUrl = await getApiUrl(); + const API_URL = `${baseUrl}/api/v1/auth/password`; - await axios.post(API_URL, payload, { + await axios.put(API_URL, payload, { headers: { Authorization: `Bearer ${data?.access_token}`, }, }); setOpen(true); - setFormData({ - oldPassword: "", - password: "", - confirmPassword: "", - }); + reset({ currentPassword: "", newPassword: "", confirmPassword: "" }); } catch (error) { const errorMessage = (error as HttpError)?.response?.data?.message; toast({ @@ -71,9 +59,6 @@ const Password = () => { } }; - const disabled = - !formData.confirmPassword || !formData.oldPassword || !formData.password; - return (
@@ -84,50 +69,96 @@ const Password = () => { Update password for enhanced account security

-
+
- - - +
+
+ +
+ +
+
+ {errors.currentPassword && ( +

+ {errors.currentPassword?.message} +

+ )} +
+
+
+ +
+ +
+
+ {errors.newPassword && ( +

+ {errors.newPassword?.message} +

+ )} +
+
+
+ +
+ +
+
+ {errors.confirmPassword && ( +

+ {errors.confirmPassword?.message} +

+ )} +
- setOpen(false)}> + setOpen(false)} + > Cancel Update Password
-
+ setOpen(!open)} show={open} />
); diff --git a/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/schema.ts b/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/schema.ts new file mode 100644 index 000000000..96f806966 --- /dev/null +++ b/src/app/dashboard/(admin)/admin/(settings)/settings/account/_component.tsx/schema.ts @@ -0,0 +1,19 @@ +import * as z from "zod"; + +export const passwordSchema = z + .object({ + currentPassword: z + .string() + .min(8, "Current password must be at least 8 characters long"), + newPassword: z + .string() + .min(8, "New password must be at least 8 characters long"), + confirmPassword: z + .string() + .min(8, "Confirm password must be at least 8 characters long"), + }) + .refine((data) => data.newPassword === data.confirmPassword, { + message: "Passwords do not match", + path: ["confirmPassword"], + }); +export type PasswordFormData = z.infer;