diff --git a/src/components/Profile/ResetPasswordForm.tsx b/src/components/Profile/ResetPasswordForm.tsx new file mode 100644 index 000000000..315232e43 --- /dev/null +++ b/src/components/Profile/ResetPasswordForm.tsx @@ -0,0 +1,118 @@ +import { useState } from 'preact/hooks'; + +export default function ResetPasswordForm() { + const [password, setPassword] = useState(''); + const [passwordConfirm, setPasswordConfirm] = useState(''); + const [isLoading, setIsLoading] = useState(false); + const [message, setMessage] = useState<{ + type: 'error' | 'success' | 'info'; + message: string; + }>(); + + const handleSubmit = async (e: Event) => { + e.preventDefault(); + setIsLoading(true); + + const urlParams = new URLSearchParams(window.location.search); + const code = urlParams.get('code'); + + const res = await fetch( + 'http://localhost:8080/v1-reset-forgotten-password', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ password, passwordConfirm, code }), + } + ); + + const data = await res.json(); + + if (!res.ok) { + setIsLoading(false); + return setMessage({ + type: 'error', + message: data.message, + }); + } + + setIsLoading(false); + setPassword(''); + setPasswordConfirm(''); + setMessage({ + type: 'success', + message: 'Your password has been reset.', + }); + + // TODO: Redirect to login page after 2 seconds + setTimeout(() => { + window.location.href = '/'; + }, 2000); + }; + + return ( +
+

+ Reset your password +

+ + + setPassword((e.target as HTMLInputElement).value)} + /> + + + + setPasswordConfirm((e.target as HTMLInputElement).value) + } + /> + + {message && ( +
+ {message.message} +
+ )} + + +
+ ); +}