'use client' import { useState } from 'react' import { useSearchParams, useRouter } from 'next/navigation' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Link2, Lock } from 'lucide-react' import Link from 'next/link' import { toast } from 'sonner' export function ResetPasswordClient() { const params = useSearchParams() const token = params.get('token') ?? '' const router = useRouter() const [password, setPassword] = useState('') const [confirm, setConfirm] = useState('') const [loading, setLoading] = useState(false) const [done, setDone] = useState(false) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() // Mirror the server's signup-identical policy (min 8 chars). if (password.length < 8) { toast.error('Password must be at least 8 characters'); return } if (password !== confirm) { toast.error('Passwords do not match'); return } setLoading(true) try { const res = await fetch('/api/password/reset', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ token, password }), }) const data = await res.json() if (res.ok) { setDone(true) toast.success('Password updated. Sign in with your new password.') router.push('/login') } else { toast.error(data?.error ?? 'Could not reset the password') } } catch { toast.error('Could not reset the password') } setLoading(false) } if (!token || done) { return (

Reset Password

This reset link is invalid or has already been used. Request a fresh one to continue.

Request a new link {' ยท '} Back to sign in

) } return (

Choose a New Password

Resetting signs out every other device.

) => setPassword(e.target.value)} className="pl-10" minLength={8} required autoFocus />
) => setConfirm(e.target.value)} className="pl-10" minLength={8} required />

Back to sign in

) }