2019-06-12 05:02:18 +00:00
|
|
|
import * as React from 'react';
|
2020-10-17 21:45:33 +00:00
|
|
|
import { useEffect, useRef, useState } from 'react';
|
2019-06-12 05:02:18 +00:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import requestPasswordResetEmail from '@/api/auth/requestPasswordResetEmail';
|
2019-06-12 06:12:03 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-06-22 20:53:41 +00:00
|
|
|
import LoginFormContainer from '@/components/auth/LoginFormContainer';
|
2020-08-02 04:08:35 +00:00
|
|
|
import { useStoreState } from 'easy-peasy';
|
2020-03-28 22:42:53 +00:00
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { Formik, FormikHelpers } from 'formik';
|
|
|
|
import { object, string } from 'yup';
|
2020-07-04 21:21:28 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Button from '@/components/elements/Button';
|
2020-08-02 04:08:35 +00:00
|
|
|
import Reaptcha from 'reaptcha';
|
|
|
|
import useFlash from '@/plugins/useFlash';
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
interface Values {
|
|
|
|
email: string;
|
|
|
|
}
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
export default () => {
|
2020-08-02 04:08:35 +00:00
|
|
|
const ref = useRef<Reaptcha>(null);
|
2022-06-26 19:13:52 +00:00
|
|
|
const [token, setToken] = useState('');
|
2020-08-02 04:08:35 +00:00
|
|
|
|
|
|
|
const { clearFlashes, addFlash } = useFlash();
|
2022-06-26 19:13:52 +00:00
|
|
|
const { enabled: recaptchaEnabled, siteKey } = useStoreState((state) => state.settings.data!.recaptcha);
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2020-10-17 21:45:33 +00:00
|
|
|
useEffect(() => {
|
|
|
|
clearFlashes();
|
|
|
|
}, []);
|
|
|
|
|
2020-03-28 22:42:53 +00:00
|
|
|
const handleSubmission = ({ email }: Values, { setSubmitting, resetForm }: FormikHelpers<Values>) => {
|
2019-06-22 23:45:51 +00:00
|
|
|
clearFlashes();
|
2020-08-02 04:08:35 +00:00
|
|
|
|
|
|
|
// 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-06-26 19:13:52 +00:00
|
|
|
ref.current!.execute().catch((error) => {
|
2020-10-17 21:30:54 +00:00
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
setSubmitting(false);
|
|
|
|
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
|
|
|
|
});
|
|
|
|
|
2020-08-02 04:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
requestPasswordResetEmail(email, token)
|
2022-06-26 19:13:52 +00:00
|
|
|
.then((response) => {
|
2020-03-28 22:42:53 +00:00
|
|
|
resetForm();
|
2019-06-22 23:45:51 +00:00
|
|
|
addFlash({ type: 'success', title: 'Success', message: response });
|
|
|
|
})
|
2022-06-26 19:13:52 +00:00
|
|
|
.catch((error) => {
|
2019-06-22 23:45:51 +00:00
|
|
|
console.error(error);
|
|
|
|
addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
|
|
|
|
})
|
2020-10-17 21:30:54 +00:00
|
|
|
.then(() => {
|
|
|
|
setToken('');
|
|
|
|
if (ref.current) ref.current.reset();
|
|
|
|
|
|
|
|
setSubmitting(false);
|
|
|
|
});
|
2019-06-12 06:12:03 +00:00
|
|
|
};
|
2019-06-12 05:02:18 +00:00
|
|
|
|
2019-06-22 23:45:51 +00:00
|
|
|
return (
|
2020-03-28 22:42:53 +00:00
|
|
|
<Formik
|
|
|
|
onSubmit={handleSubmission}
|
|
|
|
initialValues={{ email: '' }}
|
|
|
|
validationSchema={object().shape({
|
2022-06-26 19:13:52 +00:00
|
|
|
email: string()
|
|
|
|
.email('A valid email address must be provided to continue.')
|
2020-03-28 22:42:53 +00:00
|
|
|
.required('A valid email address must be provided to continue.'),
|
|
|
|
})}
|
|
|
|
>
|
2020-08-02 04:08:35 +00:00
|
|
|
{({ isSubmitting, setSubmitting, submitForm }) => (
|
2022-06-26 19:13:52 +00:00
|
|
|
<LoginFormContainer title={'Request Password Reset'} css={tw`w-full flex`}>
|
2020-03-28 22:42:53 +00:00
|
|
|
<Field
|
2020-07-04 21:21:28 +00:00
|
|
|
light
|
2020-03-28 22:42:53 +00:00
|
|
|
label={'Email'}
|
2022-06-26 19:13:52 +00:00
|
|
|
description={
|
|
|
|
'Enter your account email address to receive instructions on resetting your password.'
|
|
|
|
}
|
2020-03-28 22:42:53 +00:00
|
|
|
name={'email'}
|
|
|
|
type={'email'}
|
|
|
|
/>
|
2020-07-04 21:21:28 +00:00
|
|
|
<div css={tw`mt-6`}>
|
2022-06-26 19:13:52 +00:00
|
|
|
<Button type={'submit'} size={'xlarge'} disabled={isSubmitting} isLoading={isSubmitting}>
|
2020-07-04 21:21:28 +00:00
|
|
|
Send Email
|
|
|
|
</Button>
|
2020-03-28 22:42:53 +00:00
|
|
|
</div>
|
2022-06-26 19:13:52 +00:00
|
|
|
{recaptchaEnabled && (
|
|
|
|
<Reaptcha
|
|
|
|
ref={ref}
|
|
|
|
size={'invisible'}
|
|
|
|
sitekey={siteKey || '_invalid_key'}
|
|
|
|
onVerify={(response) => {
|
|
|
|
setToken(response);
|
|
|
|
submitForm();
|
|
|
|
}}
|
|
|
|
onExpire={() => {
|
|
|
|
setSubmitting(false);
|
|
|
|
setToken('');
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
2020-07-04 21:21:28 +00:00
|
|
|
<div css={tw`mt-6 text-center`}>
|
2020-03-28 22:42:53 +00:00
|
|
|
<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`}
|
2020-03-28 22:42:53 +00:00
|
|
|
>
|
|
|
|
Return to Login
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</LoginFormContainer>
|
|
|
|
)}
|
|
|
|
</Formik>
|
2019-06-22 23:45:51 +00:00
|
|
|
);
|
2019-06-12 06:12:03 +00:00
|
|
|
};
|