"use client" import { useState } from "react" import { zodResolver } from "@hookform/resolvers/zod" import { useForm } from "react-hook-form" import { Eye, EyeOff, Loader2, Lock, Save } from "lucide-react" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" import { Input } from "@/components/ui/input" import { passwordChangeSchema, type PasswordChangeInput } from "@/lib/validation" export function PasswordForm() { const [showCurrentPassword, setShowCurrentPassword] = useState(false) const [showNewPassword, setShowNewPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const form = useForm({ resolver: zodResolver(passwordChangeSchema), defaultValues: { currentPassword: "", newPassword: "", confirmPassword: "", }, }) const onSubmit = async (data: PasswordChangeInput) => { setIsLoading(true) try { const response = await fetch("/api/user/password", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ currentPassword: data.currentPassword, newPassword: data.newPassword, }), }) const result = await response.json() if (!response.ok) { toast.error(result.error || "Failed to update password") return } toast.success("Password updated successfully!") form.reset() } catch { toast.error("An unexpected error occurred") } finally { setIsLoading(false) } } return ( Change Password Update your password to keep your account secure
( Current Password
)} /> ( New Password
)} /> ( Confirm New Password
)} />
Password must contain at least 8 characters with uppercase, lowercase, and a number.
) }