"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2Icon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { changePassword } from "@/app/(frontend)/account/actions"; export function ChangePasswordForm() { const router = useRouter(); const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [error, setError] = useState(null); const [success, setSuccess] = useState(null); const [loading, setLoading] = useState(false); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setSuccess(null); if (newPassword !== confirmPassword) { setError("New passwords do not match."); return; } setLoading(true); try { const res = await changePassword({ currentPassword, newPassword }); if (!res.success) { setError(res.error ?? "Failed to change password."); return; } setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); setSuccess("Password updated."); router.refresh(); } catch { setError("Something went wrong. Please try again."); } finally { setLoading(false); } }; return (
setCurrentPassword(e.target.value)} autoComplete="current-password" required />
setNewPassword(e.target.value)} autoComplete="new-password" required />
setConfirmPassword(e.target.value)} autoComplete="new-password" required />
{error &&

{error}

} {success &&

{success}

}
); }