"use client"; import { useEffect, useState, Suspense } from "react"; import { useSearchParams, useRouter } from "next/navigation"; import Link from "next/link"; function ResetPasswordContent() { const searchParams = useSearchParams(); const router = useRouter(); const token = searchParams.get('token'); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [validating, setValidating] = useState(true); const [tokenValid, setTokenValid] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); // Validate token on mount useEffect(() => { if (!token) { setError('Invalid reset link'); setValidating(false); return; } const validateToken = async () => { try { const response = await fetch(`/api/auth/reset-password?token=${token}`); const data = await response.json(); if (data.valid) { setTokenValid(true); } else { setError('This reset link is invalid or has expired'); } } catch (err) { setError('Failed to validate reset link'); } finally { setValidating(false); } }; validateToken(); }, [token]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(''); if (newPassword !== confirmPassword) { setError('Passwords do not match'); return; } if (newPassword.length < 6) { setError('Password must be at least 6 characters'); return; } setLoading(true); try { const response = await fetch('/api/auth/reset-password', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, newPassword }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to reset password'); } setSuccess(true); // Redirect to login after 3 seconds setTimeout(() => { router.push('/login'); }, 3000); } catch (err: any) { setError(err.message || 'An error occurred'); } finally { setLoading(false); } }; if (validating) { return (
Validating reset link...
{error || 'This password reset link is invalid or has expired.'}
Request New Reset Link →Your password has been reset successfully. Redirecting to login...
Go to Login →Enter your new password below.
{error && (Loading...
}>