'use client';
import { useState, useEffect } from 'react';
import { useSearchParams, useRouter } from 'next/navigation';
import {
Box,
TextField,
Button,
Typography,
Paper,
Alert,
CircularProgress,
InputAdornment,
IconButton,
Link as MuiLink,
} from '@mui/material';
import { LockReset, Visibility, VisibilityOff, CheckCircle } from '@mui/icons-material';
import { motion } from 'framer-motion';
import Link from 'next/link';
import apiClient from '@/lib/api/client';
import { extractError } from '@/lib/utils/errorHandler';
export default function ResetPasswordPage() {
const searchParams = useSearchParams();
const router = useRouter();
const [token, setToken] = useState('');
const [newPassword, setNewPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [showConfirmPassword, setShowConfirmPassword] = useState(false);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [success, setSuccess] = useState(false);
useEffect(() => {
const tokenParam = searchParams.get('token');
if (tokenParam) {
setToken(tokenParam);
} else {
setError('Invalid or missing reset token');
}
}, [searchParams]);
const validatePassword = (password: string): string | null => {
if (password.length < 8) {
return 'Password must be at least 8 characters long';
}
if (!/[a-z]/.test(password)) {
return 'Password must contain at least one lowercase letter';
}
if (!/[A-Z]/.test(password)) {
return 'Password must contain at least one uppercase letter';
}
if (!/\d/.test(password)) {
return 'Password must contain at least one number';
}
return null;
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
// Validation
if (!newPassword.trim()) {
setError('Please enter a new password');
return;
}
const passwordError = validatePassword(newPassword);
if (passwordError) {
setError(passwordError);
return;
}
if (newPassword !== confirmPassword) {
setError('Passwords do not match');
return;
}
setLoading(true);
setError('');
try {
await apiClient.post('/api/v1/auth/password/reset', {
token,
newPassword,
});
setSuccess(true);
// Redirect to login after 3 seconds
setTimeout(() => {
router.push('/login');
}, 3000);
} catch (err: any) {
console.error('Reset password error:', err);
const errorData = extractError(err);
setError(errorData.userMessage || errorData.message);
} finally {
setLoading(false);
}
};
if (!token && !error) {
return (
);
}
return (
{!success ? (
<>
Reset Password
Enter your new password below
{error && (
{error}
)}
Back to Login
>
) : (
Password Reset Successful! 🎉
Your password has been reset successfully. You can now log in with your new password.
Redirecting to login page...
)}
);
}