import { type FormEvent, useEffect, useState } from 'react'; import { httpPost } from '../../lib/http'; import Cookies from 'js-cookie'; import { TOKEN_COOKIE_NAME, setAuthToken } from '../../lib/jwt'; export function ResetPasswordForm() { const [code, setCode] = useState(''); const [password, setPassword] = useState(''); const [passwordConfirm, setPasswordConfirm] = useState(''); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(''); useEffect(() => { const urlParams = new URLSearchParams(window.location.search); const code = urlParams.get('code'); if (!code) { window.location.href = '/login'; } else { setCode(code); } }, []); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); if (password !== passwordConfirm) { setIsLoading(false); setError('Passwords do not match.'); return; } const { response, error } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-reset-forgotten-password`, { newPassword: password, confirmPassword: passwordConfirm, code, }, ); if (error?.message) { setIsLoading(false); setError(error.message); return; } if (!response?.token) { setIsLoading(false); setError('Something went wrong. Please try again later.'); return; } const token = response.token; setAuthToken(response.token); window.location.href = '/'; }; return (
setPassword((e.target as HTMLInputElement).value)} /> setPasswordConfirm((e.target as HTMLInputElement).value) } /> {error && (

{error}

)}
); }