'use client' import React, { useState, useEffect } from 'react' import { useSearchParams, useRouter } from 'next/navigation' import { AuthLayout } from '@/components/layouts/AuthLayout' import { Input } from '@/components/common/Input' import { Button } from '@/components/common/Button' import { Link } from '@/components/common/Link' import { useApi } from '@/lib/hooks/useApi' export default function ResetPasswordConfirmPage() { const searchParams = useSearchParams() const router = useRouter() const { execute, isLoading, error } = useApi() const [token, setToken] = useState('') const [formData, setFormData] = useState({ password: '', confirmPassword: '', }) const [formErrors, setFormErrors] = useState>({}) const [success, setSuccess] = useState(false) useEffect(() => { const tokenParam = searchParams.get('token') if (tokenParam) { setToken(tokenParam) } else { // Redirect to reset password page if no token router.push('/auth/reset-password') } }, [searchParams, router]) const validateForm = () => { const errors: Record = {} if (!formData.password) { errors.password = 'Password is required' } else if (formData.password.length < 8) { errors.password = 'Password must be at least 8 characters' } else if (!/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)/.test(formData.password)) { errors.password = 'Password must contain uppercase, lowercase, and number' } if (!formData.confirmPassword) { errors.confirmPassword = 'Please confirm your password' } else if (formData.password !== formData.confirmPassword) { errors.confirmPassword = 'Passwords do not match' } setFormErrors(errors) return Object.keys(errors).length === 0 } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!validateForm()) { return } const data = await execute({ method: 'POST', url: '/auth/reset-password/confirm', data: { token, password: formData.password, }, }) if (data) { setSuccess(true) } } const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target setFormData((prev) => ({ ...prev, [name]: value, })) // Clear error for this field if (formErrors[name]) { setFormErrors((prev) => { const newErrors = { ...prev } delete newErrors[name] return newErrors }) } } if (success) { return (

Your password has been successfully reset. You can now sign in with your new password.

) } return (
{error && (

{error.message || 'Failed to reset password. The link may have expired.'}

)}
Remember your password?{' '} Sign in
) }