'use client'; import React, { useState, useEffect } from 'react'; import Link from 'next/link'; import { useSearchParams, useRouter } from 'next/navigation'; import { Card, CardContent } from '@/components/ui/Card'; import { Input } from '@/components/ui/Input'; import { Button } from '@/components/ui/Button'; import { useCsrf } from '@/hooks/useCsrf'; import { Suspense } from 'react'; function ResetPasswordContent() { const { fetchWithCsrf, loading: csrfLoading } = useCsrf(); const searchParams = useSearchParams(); const router = useRouter(); const [token, setToken] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(false); const [error, setError] = useState(''); useEffect(() => { const tokenParam = searchParams.get('token'); if (!tokenParam) { setError('Invalid or missing reset token. Please request a new password reset link.'); } else { setToken(tokenParam); } }, [searchParams]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); // Validate passwords match if (password !== confirmPassword) { setError('Passwords do not match'); setLoading(false); return; } // Validate password length if (password.length < 8) { setError('Password must be at least 8 characters long'); setLoading(false); return; } try { const response = await fetchWithCsrf('/api/auth/reset-password', { method: 'POST', body: JSON.stringify({ token, password }), }); const data = await response.json(); if (response.ok) { setSuccess(true); // Redirect to login after 3 seconds setTimeout(() => { router.push('/login'); }, 3000); } else { setError(data.error || 'Failed to reset password'); } } catch (err) { setError('An error occurred. Please try again.'); } finally { setLoading(false); } }; if (success) { return (
Your password has been updated
Your password has been successfully reset!
Redirecting you to the login page in 3 seconds...
Enter your new password below
{error}
Remember your password?{' '} Sign in