misc_pterodactyl-panel/resources/scripts/components/auth/ForgotPasswordContainer.tsx

123 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import { useStoreState } from 'easy-peasy';
import type { FormikHelpers } from 'formik';
import { Formik } from 'formik';
import { useEffect, useRef, useState } from 'react';
2019-06-12 05:02:18 +00:00
import { Link } from 'react-router-dom';
2022-11-25 20:25:03 +00:00
import Reaptcha from 'reaptcha';
import tw from 'twin.macro';
import { object, string } from 'yup';
2019-06-12 05:02:18 +00:00
import requestPasswordResetEmail from '@/api/auth/requestPasswordResetEmail';
import { httpErrorToHuman } from '@/api/http';
2019-06-22 20:53:41 +00:00
import LoginFormContainer from '@/components/auth/LoginFormContainer';
2020-07-04 21:21:28 +00:00
import Button from '@/components/elements/Button';
2022-11-25 20:25:03 +00:00
import Field from '@/components/elements/Field';
import useFlash from '@/plugins/useFlash';
2019-06-12 05:02:18 +00:00
interface Values {
email: string;
}
2019-06-12 05:02:18 +00:00
2022-11-25 20:25:03 +00:00
function ForgotPasswordContainer() {
const ref = useRef<Reaptcha>(null);
const [token, setToken] = useState('');
const { clearFlashes, addFlash } = useFlash();
2022-11-25 20:25:03 +00:00
const { enabled: recaptchaEnabled, siteKey } = useStoreState(state => state.settings.data!.recaptcha);
2019-06-12 05:02:18 +00:00
useEffect(() => {
clearFlashes();
}, []);
const handleSubmission = ({ email }: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
clearFlashes();
// If there is no token in the state yet, request the token and then abort this submit request
// since it will be re-submitted when the recaptcha data is returned by the component.
if (recaptchaEnabled && !token) {
2022-11-25 20:25:03 +00:00
ref.current!.execute().catch(error => {
console.error(error);
setSubmitting(false);
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
});
return;
}
requestPasswordResetEmail(email, token)
2022-11-25 20:25:03 +00:00
.then(response => {
resetForm();
addFlash({ type: 'success', title: 'Success', message: response });
})
2022-11-25 20:25:03 +00:00
.catch(error => {
console.error(error);
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
})
.then(() => {
setToken('');
2022-11-25 20:25:03 +00:00
if (ref.current !== null) {
void ref.current.reset();
}
setSubmitting(false);
});
};
2019-06-12 05:02:18 +00:00
return (
<Formik
onSubmit={handleSubmission}
initialValues={{ email: '' }}
validationSchema={object().shape({
email: string()
.email('A valid email address must be provided to continue.')
.required('A valid email address must be provided to continue.'),
})}
>
{({ isSubmitting, setSubmitting, submitForm }) => (
<LoginFormContainer title={'Request Password Reset'} css={tw`w-full flex`}>
<Field
2020-07-04 21:21:28 +00:00
light
label={'Email'}
description={
'Enter your account email address to receive instructions on resetting your password.'
}
name={'email'}
type={'email'}
/>
2020-07-04 21:21:28 +00:00
<div css={tw`mt-6`}>
<Button type={'submit'} size={'xlarge'} disabled={isSubmitting} isLoading={isSubmitting}>
2020-07-04 21:21:28 +00:00
Send Email
</Button>
</div>
{recaptchaEnabled && (
<Reaptcha
ref={ref}
size={'invisible'}
sitekey={siteKey || '_invalid_key'}
2022-11-25 20:25:03 +00:00
onVerify={response => {
setToken(response);
2022-11-25 20:25:03 +00:00
void submitForm();
}}
onExpire={() => {
setSubmitting(false);
setToken('');
}}
/>
)}
2020-07-04 21:21:28 +00:00
<div css={tw`mt-6 text-center`}>
<Link
to={'/auth/login'}
2020-07-04 21:21:28 +00:00
css={tw`text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700`}
>
Return to Login
</Link>
</div>
</LoginFormContainer>
)}
</Formik>
);
2022-11-25 20:25:03 +00:00
}
export default ForgotPasswordContainer;