import React, { useState } from 'react'; import { Link, RouteComponentProps } from 'react-router-dom'; import loginCheckpoint from '@/api/auth/loginCheckpoint'; import LoginFormContainer from '@/components/auth/LoginFormContainer'; import { ActionCreator } from 'easy-peasy'; import { StaticContext } from 'react-router'; import { useFormikContext, withFormik } from 'formik'; import useFlash from '@/plugins/useFlash'; import { FlashStore } from '@/state/flashes'; import Field from '@/components/elements/Field'; import tw from 'twin.macro'; import Button from '@/components/elements/Button'; interface Values { code: string; recoveryCode: ''; } type OwnProps = RouteComponentProps, StaticContext, { token?: string }>; type Props = OwnProps & { clearAndAddHttpError: ActionCreator; }; const LoginCheckpointContainer = () => { const { isSubmitting, setFieldValue } = useFormikContext(); const [isMissingDevice, setIsMissingDevice] = useState(false); return (
{ setFieldValue('code', ''); setFieldValue('recoveryCode', ''); setIsMissingDevice((s) => !s); }} css={tw`cursor-pointer text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700`} > {!isMissingDevice ? "I've Lost My Device" : 'I Have My Device'}
Return to Login
); }; const EnhancedForm = withFormik({ handleSubmit: ({ code, recoveryCode }, { setSubmitting, props: { clearAndAddHttpError, location } }) => { loginCheckpoint(location.state?.token || '', code, recoveryCode) .then((response) => { if (response.complete) { // @ts-expect-error this is valid window.location = response.intended || '/'; return; } setSubmitting(false); }) .catch((error) => { console.error(error); setSubmitting(false); clearAndAddHttpError({ error }); }); }, mapPropsToValues: () => ({ code: '', recoveryCode: '', }), })(LoginCheckpointContainer); export default ({ history, location, ...props }: OwnProps) => { const { clearAndAddHttpError } = useFlash(); if (!location.state?.token) { history.replace('/auth/login'); return null; } return ( ); };