'use client'; import { useState, useTransition, useEffect } from 'react'; import { useTranslations } from 'next-intl'; import { useParams, useRouter } from 'next/navigation'; import Link from 'next/link'; import { TesseraLogo } from '@/components/brand/tessera-logo'; const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001'; /** * Password reset form with token validation (D-03 self-service). * User arrives here from the reset email link. * On success, redirects to /login after 3 seconds. * Part of (auth) route group -- no sidebar/header (D-04). */ export default function ResetPasswordTokenPage() { const t = useTranslations('auth'); const router = useRouter(); const params = useParams(); const token = params.token as string; const [isPending, startTransition] = useTransition(); const [success, setSuccess] = useState(false); const [error, setError] = useState(null); const [passwordMismatch, setPasswordMismatch] = useState(false); // Redirect to login after successful reset useEffect(() => { if (!success) return; const timer = setTimeout(() => { router.push('/login'); }, 3000); return () => clearTimeout(timer); }, [success, router]); async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setError(null); setPasswordMismatch(false); const formData = new FormData(e.currentTarget); const newPassword = formData.get('newPassword') as string; const confirmPassword = formData.get('confirmPassword') as string; if (newPassword !== confirmPassword) { setPasswordMismatch(true); return; } startTransition(async () => { try { const response = await fetch(`${API_URL}/auth/reset-password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, newPassword }), }); if (!response.ok) { const data = await response.json().catch(() => null); const message = data?.message || ''; if (message.includes('expired')) { setError('tokenExpired'); } else if (message.includes('used')) { setError('tokenUsed'); } else { setError('tokenInvalid'); } return; } setSuccess(true); } catch { setError('networkError'); } }); } return (
{/* Heading */}

{t('resetPassword.newPasswordTitle')}

{success ? ( /* Success message with redirect notice */
{t('resetPassword.resetSuccess')}

{t('resetPassword.redirecting')}

{t('resetPassword.backToLogin')}
) : ( <> {/* Error message */} {error && (
{t(`resetPassword.${error}`)}
)} {/* Password mismatch */} {passwordMismatch && (
{t('resetPassword.passwordMismatch')}
)}
{/* New password */}
{/* Confirm password */}
{t('resetPassword.backToLogin')}
)}
); }