'use client'; import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { AlertTriangle, Eye, EyeOff, KeyRound } from 'lucide-react'; import { changeOwnPassword } from '@/actions/users'; interface Props { mustChangePassword: boolean; } export function ChangePasswordForm({ mustChangePassword }: Props) { const router = useRouter(); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [showCurrent, setShowCurrent] = useState(false); const [showNew, setShowNew] = useState(false); const [showConfirm, setShowConfirm] = useState(false); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [loading, setLoading] = useState(false); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); if (newPassword.length < 8) { setError('New password must be at least 8 characters.'); return; } if (newPassword !== confirmPassword) { setError('New passwords do not match.'); return; } setLoading(true); try { await changeOwnPassword({ currentPassword, newPassword }); setSuccess(true); setCurrentPassword(''); setNewPassword(''); setConfirmPassword(''); // Reload so the session banner clears router.refresh(); } catch (err: unknown) { setError(err instanceof Error ? err.message : 'Failed to change password.'); } finally { setLoading(false); } } return (
{mustChangePassword && (
You must set a new password before continuing. Your account was created with a temporary password.
)} Change Password {success ? (

Password updated successfully.

) : (
setCurrentPassword(e.target.value)} required autoComplete="current-password" className="pr-10" />
setNewPassword(e.target.value)} required autoComplete="new-password" className="pr-10" />

At least 8 characters

setConfirmPassword(e.target.value)} required autoComplete="new-password" className="pr-10" />
{error && (

{error}

)}
)}
); }