import { useState, useEffect } from 'react'; import { useResetPassword, useAuthStore } from '@imphnen-frontend-service/service'; import { useNavigate } from 'react-router'; import { toast } from 'sonner'; import { Icon } from '@iconify/react'; export default function ResetPasswordPage() { const navigate = useNavigate(); const { clearSession } = useAuthStore(); const resetPasswordMutation = useResetPassword(); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [accessToken, setAccessToken] = useState(null); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); useEffect(() => { // Get the access_token from URL hash (Supabase sends it as hash fragment) // or from query params (when redirected from callback page) const hashParams = new URLSearchParams(globalThis.location.hash.substring(1)); const queryParams = new URLSearchParams(globalThis.location.search); const token = hashParams.get('access_token') || queryParams.get('access_token'); if (token) { setAccessToken(token); } else { toast.error('Invalid or expired reset link'); setTimeout(() => navigate('/auth/forgot-password'), 2000); } }, [navigate]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (password !== confirmPassword) { toast.error('Passwords do not match'); return; } if (password.length < 6) { toast.error('Password must be at least 6 characters'); return; } if (!accessToken) { toast.error('Invalid reset token'); return; } try { await resetPasswordMutation.mutateAsync({ access_token: accessToken, new_password: password, }); toast.success('Password updated successfully!'); // Clear session and redirect to login clearSession(); navigate('/auth/login'); } catch (err) { toast.error((err as Error).message || 'Failed to reset password'); } }; if (!accessToken) { return (

Verifying reset link...

); } return (

Set New Password

Enter your new password below

setPassword(e.target.value)} placeholder="••••••••" disabled={resetPasswordMutation.isPending} className="w-full px-4 py-2.5 pr-12 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent disabled:bg-gray-100 disabled:cursor-not-allowed bg-white dark:bg-gray-800 text-gray-900 dark:text-white" required minLength={6} />
setConfirmPassword(e.target.value)} placeholder="••••••••" disabled={resetPasswordMutation.isPending} className="w-full px-4 py-2.5 pr-12 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent disabled:bg-gray-100 disabled:cursor-not-allowed bg-white dark:bg-gray-800 text-gray-900 dark:text-white" required minLength={6} />
); }