"use client" import type React from "react" import { useState, useEffect, Suspense } from "react" import { useRouter, useSearchParams } from "next/navigation" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Alert, AlertDescription } from "@/components/ui/alert" import { Eye, EyeOff, Loader2, CheckCircle, Lock, ArrowLeft, Cpu } from "lucide-react" function ResetPasswordContent() { const [password, setPassword] = useState("") const [confirmPassword, setConfirmPassword] = useState("") const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState("") const [success, setSuccess] = useState(false) const router = useRouter() const searchParams = useSearchParams() const token = searchParams.get("token") const email = searchParams.get("email") useEffect(() => { if (!token || !email) { setError("Invalid or missing reset credentials. Please request a new password reset.") } }, [token, email]) const handleGoBackToVerification = () => { // Redirect back to forgot password page with email pre-filled router.push(`/forgot-password?email=${encodeURIComponent(email || '')}`) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError("") setSuccess(false) // Validation if (!password || !confirmPassword) { setError("Both password fields are required") return } if (password.length < 6) { setError("Password must be at least 6 characters long") return } if (password !== confirmPassword) { setError("Passwords do not match") return } if (!token || !email) { setError("Invalid reset credentials") return } setIsLoading(true) try { // Ensure we have valid data before sending if (!email || !token || !password) { setError("Email, token, and password are required") setIsLoading(false) return } const requestBody = JSON.stringify({ email: email.trim(), token: token.trim(), password }) const response = await fetch("/api/auth/reset-password", { method: "POST", headers: { "Content-Type": "application/json", }, body: requestBody, }) // Check if response has valid JSON const contentType = response.headers.get('content-type') let data if (contentType && contentType.includes('application/json')) { data = await response.json() } else { const text = await response.text() throw new Error(text || `Server error: ${response.status}`) } if (!response.ok) { // Show detailed error message const errorMessage = data.error || "Failed to reset password" const errorDetails = data.details ? ` ${data.details}` : '' throw new Error(`${errorMessage}${errorDetails}`) } setSuccess(true) setTimeout(() => { router.push("/login") }, 2000) } catch (err: any) { console.error("Reset password error:", err) setError(err.message || "Failed to reset password. Please try again.") } finally { setIsLoading(false) } } return (
Enter your new password