2019-12-15 18:05:44 -08:00
|
|
|
import React, { useRef } from 'react';
|
2019-06-22 17:45:32 -07:00
|
|
|
import { Link, RouteComponentProps } from 'react-router-dom';
|
2019-12-15 18:05:44 -08:00
|
|
|
import login, { LoginData } from '@/api/auth/login';
|
2019-06-22 13:53:41 -07:00
|
|
|
import LoginFormContainer from '@/components/auth/LoginFormContainer';
|
2019-12-15 18:05:44 -08:00
|
|
|
import { ActionCreator, Actions, useStoreActions, useStoreState } from 'easy-peasy';
|
2019-07-09 21:25:57 -07:00
|
|
|
import { ApplicationStore } from '@/state';
|
2019-12-15 16:41:20 -08:00
|
|
|
import { FormikProps, withFormik } from 'formik';
|
|
|
|
import { object, string } from 'yup';
|
|
|
|
import Field from '@/components/elements/Field';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-12-15 18:05:44 -08:00
|
|
|
import { FlashMessage } from '@/state/flashes';
|
|
|
|
import ReCAPTCHA from 'react-google-recaptcha';
|
2019-12-22 14:53:27 -08:00
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
type OwnProps = RouteComponentProps & {
|
2019-12-15 18:05:44 -08:00
|
|
|
clearFlashes: ActionCreator<void>;
|
|
|
|
addFlash: ActionCreator<FlashMessage>;
|
2019-12-15 16:41:20 -08:00
|
|
|
}
|
|
|
|
|
2019-12-15 18:05:44 -08:00
|
|
|
const LoginContainer = ({ isSubmitting, setFieldValue, values, submitForm, handleSubmit }: OwnProps & FormikProps<LoginData>) => {
|
|
|
|
const ref = useRef<ReCAPTCHA | null>(null);
|
|
|
|
const { enabled: recaptchaEnabled, siteKey } = useStoreState<ApplicationStore, any>(state => state.settings.data!.recaptcha);
|
|
|
|
|
|
|
|
const submit = (e: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (ref.current && !values.recaptchaData) {
|
|
|
|
return ref.current.execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSubmit(e);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{ref.current && ref.current.render()}
|
2020-03-28 15:42:53 -07:00
|
|
|
<LoginFormContainer
|
|
|
|
title={'Login to Continue'}
|
|
|
|
className={'w-full flex'}
|
|
|
|
onSubmit={submit}
|
|
|
|
>
|
|
|
|
<label htmlFor={'username'}>Username or Email</label>
|
|
|
|
<Field
|
|
|
|
type={'text'}
|
|
|
|
id={'username'}
|
|
|
|
name={'username'}
|
|
|
|
className={'input'}
|
|
|
|
/>
|
|
|
|
<div className={'mt-6'}>
|
|
|
|
<label htmlFor={'password'}>Password</label>
|
2019-12-15 18:05:44 -08:00
|
|
|
<Field
|
2020-03-28 15:42:53 -07:00
|
|
|
type={'password'}
|
|
|
|
id={'password'}
|
|
|
|
name={'password'}
|
2019-12-15 18:05:44 -08:00
|
|
|
className={'input'}
|
|
|
|
/>
|
2020-03-28 15:42:53 -07:00
|
|
|
</div>
|
|
|
|
<div className={'mt-6'}>
|
|
|
|
<button
|
|
|
|
type={'submit'}
|
|
|
|
className={'btn btn-primary btn-jumbo'}
|
|
|
|
>
|
|
|
|
{isSubmitting ?
|
|
|
|
<Spinner size={'tiny'} className={'mx-auto'}/>
|
|
|
|
:
|
|
|
|
'Login'
|
|
|
|
}
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
{recaptchaEnabled &&
|
|
|
|
<ReCAPTCHA
|
|
|
|
ref={ref}
|
|
|
|
size={'invisible'}
|
|
|
|
sitekey={siteKey || '_invalid_key'}
|
|
|
|
onChange={token => {
|
|
|
|
ref.current && ref.current.reset();
|
|
|
|
setFieldValue('recaptchaData', token);
|
|
|
|
submitForm();
|
|
|
|
}}
|
|
|
|
onExpired={() => setFieldValue('recaptchaData', null)}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
<div className={'mt-6 text-center'}>
|
|
|
|
<Link
|
|
|
|
to={'/auth/password'}
|
|
|
|
className={'text-xs text-neutral-500 tracking-wide no-underline uppercase hover:text-neutral-600'}
|
|
|
|
>
|
|
|
|
Forgot password?
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</LoginFormContainer>
|
2019-12-15 18:05:44 -08:00
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
};
|
2019-12-15 16:41:20 -08:00
|
|
|
|
2019-12-15 18:05:44 -08:00
|
|
|
const EnhancedForm = withFormik<OwnProps, LoginData>({
|
2019-12-15 16:41:20 -08:00
|
|
|
displayName: 'LoginContainerForm',
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
mapPropsToValues: (props) => ({
|
|
|
|
username: '',
|
|
|
|
password: '',
|
2019-12-15 18:05:44 -08:00
|
|
|
recaptchaData: null,
|
2019-12-15 16:41:20 -08:00
|
|
|
}),
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
validationSchema: () => object().shape({
|
|
|
|
username: string().required('A username or email must be provided.'),
|
|
|
|
password: string().required('Please enter your account password.'),
|
|
|
|
}),
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 18:05:44 -08:00
|
|
|
handleSubmit: (values, { props, setFieldValue, setSubmitting }) => {
|
2019-12-15 16:41:20 -08:00
|
|
|
props.clearFlashes();
|
2019-12-15 18:05:44 -08:00
|
|
|
login(values)
|
2019-06-22 16:45:51 -07:00
|
|
|
.then(response => {
|
|
|
|
if (response.complete) {
|
|
|
|
// @ts-ignore
|
|
|
|
window.location = response.intended || '/';
|
|
|
|
return;
|
|
|
|
}
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
props.history.replace('/auth/login/checkpoint', { token: response.confirmationToken });
|
2019-06-22 16:45:51 -07:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
setSubmitting(false);
|
2019-12-15 18:05:44 -08:00
|
|
|
setFieldValue('recaptchaData', null);
|
2019-12-15 16:41:20 -08:00
|
|
|
props.addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) });
|
2019-06-22 16:45:51 -07:00
|
|
|
});
|
2019-12-15 16:41:20 -08:00
|
|
|
},
|
|
|
|
})(LoginContainer);
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-12-15 16:41:20 -08:00
|
|
|
export default (props: RouteComponentProps) => {
|
|
|
|
const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
|
2019-06-09 19:26:20 -07:00
|
|
|
|
2019-06-22 16:45:51 -07:00
|
|
|
return (
|
2019-12-15 16:41:20 -08:00
|
|
|
<EnhancedForm
|
|
|
|
{...props}
|
|
|
|
addFlash={addFlash}
|
|
|
|
clearFlashes={clearFlashes}
|
|
|
|
/>
|
2019-06-22 16:45:51 -07:00
|
|
|
);
|
|
|
|
};
|