From 4ef6eb0e20a041cd01dc5c8aa58693f0cf7f246b Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Sun, 2 Apr 2023 01:40:31 +0600 Subject: [PATCH] chore: reset password page --- src/components/Profile/ResetPasswordForm.tsx | 118 +++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/components/Profile/ResetPasswordForm.tsx 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} +
+ )} + + +
+ ); +}