/** * Reset Password Page * * Reset password with token from email */ 'use client'; import { useState, useEffect } from 'react'; import { useSearchParams, useRouter } from 'next/navigation'; import { authApi } from '@/lib/api'; import Link from 'next/link'; export default function ResetPasswordPage() { const searchParams = useSearchParams(); const router = useRouter(); const [token, setToken] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [success, setSuccess] = useState(false); const [error, setError] = useState(''); const [loading, setLoading] = useState(false); useEffect(() => { const tokenFromUrl = searchParams.get('token'); if (tokenFromUrl) { setToken(tokenFromUrl); } else { setError('Invalid reset link. Please request a new password reset.'); } }, [searchParams]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); // Validate passwords match if (password !== confirmPassword) { setError('Passwords do not match'); return; } // Validate password length if (password.length < 12) { setError('Password must be at least 12 characters long'); return; } if (!token) { setError('Invalid reset token'); return; } setLoading(true); try { await authApi.resetPassword(token, password); setSuccess(true); setTimeout(() => { router.push('/login'); }, 3000); } catch (err: any) { setError( err.response?.data?.message || 'Failed to reset password. The link may have expired.' ); } finally { setLoading(false); } }; if (success) { return (
Please enter your new password.