import React from 'react'; import { Link, RouteComponentProps } from 'react-router-dom'; import loginCheckpoint from '@/api/auth/loginCheckpoint'; import { httpErrorToHuman } from '@/api/http'; import LoginFormContainer from '@/components/auth/LoginFormContainer'; import { ActionCreator } from 'easy-peasy'; import { StaticContext } from 'react-router'; import Spinner from '@/components/elements/Spinner'; import { useFormikContext, withFormik } from 'formik'; import { object, string } from 'yup'; import useFlash from '@/plugins/useFlash'; import { FlashStore } from '@/state/flashes'; import Field from '@/components/elements/Field'; interface Values { code: string; } type OwnProps = RouteComponentProps<{}, StaticContext, { token?: string }> type Props = OwnProps & { addError: ActionCreator; clearFlashes: ActionCreator; } const LoginCheckpointContainer = () => { const { isSubmitting } = useFormikContext(); return (
Return to Login
); }; const EnhancedForm = withFormik({ handleSubmit: ({ code }, { setSubmitting, props: { addError, clearFlashes, location } }) => { clearFlashes(); console.log(location.state.token, code); loginCheckpoint(location.state?.token || '', code) .then(response => { if (response.complete) { // @ts-ignore window.location = response.intended || '/'; return; } setSubmitting(false); }) .catch(error => { console.error(error); setSubmitting(false); addError({ message: httpErrorToHuman(error) }); }); }, mapPropsToValues: () => ({ code: '', }), validationSchema: object().shape({ code: string().required('An authentication code must be provided.') .length(6, 'Authentication code must be 6 digits in length.'), }), })(LoginCheckpointContainer); export default ({ history, location, ...props }: OwnProps) => { const { addError, clearFlashes } = useFlash(); if (!location.state?.token) { history.replace('/auth/login'); return null; } return ; };