'use client'; import { useState, useTransition, useEffect } from 'react'; import { useTranslations } from 'next-intl'; import { fetchCurrentUser, changePasswordAction } from '@/lib/auth-actions'; export default function ChangePasswordPage() { const t = useTranslations('auth'); const [isPending, startTransition] = useTransition(); const [error, setError] = useState(null); const [passwordMismatch, setPasswordMismatch] = useState(false); const [isForced, setIsForced] = useState(false); useEffect(() => { async function checkForceChange() { const currentUser = await fetchCurrentUser(); if (currentUser?.mustChangePassword) { setIsForced(true); } } checkForceChange(); }, []); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setPasswordMismatch(false); const formData = new FormData(e.currentTarget); const currentPassword = formData.get('currentPassword') as string; const newPassword = formData.get('newPassword') as string; const confirmPassword = formData.get('confirmPassword') as string; if (newPassword !== confirmPassword) { setPasswordMismatch(true); return; } startTransition(async () => { const result = await changePasswordAction(currentPassword, newPassword); if (result) { setError(result.error); } }); } return (

{t('changePassword.title')}

{isForced && (
{t('changePassword.forceChangeNotice')}
)} {error && (
{t(`changePassword.${error}`)}
)} {passwordMismatch && (
{t('changePassword.passwordMismatch')}
)}
); }