|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React from 'react'; |
2 | 2 | import { Link, RouteComponentProps } from 'react-router-dom'; |
3 | 3 | import loginCheckpoint from '@/api/auth/loginCheckpoint'; |
4 | 4 | import { httpErrorToHuman } from '@/api/http'; |
5 | 5 | import LoginFormContainer from '@/components/auth/LoginFormContainer'; |
6 | | -import { Actions, useStoreActions } from 'easy-peasy'; |
| 6 | +import { ActionCreator } from 'easy-peasy'; |
7 | 7 | import { StaticContext } from 'react-router'; |
8 | | -import FlashMessageRender from '@/components/FlashMessageRender'; |
9 | | -import { ApplicationStore } from '@/state'; |
10 | 8 | import Spinner from '@/components/elements/Spinner'; |
11 | | -import styled from 'styled-components'; |
12 | | -import { breakpoint } from 'styled-components-breakpoint'; |
| 9 | +import { useFormikContext, withFormik } from 'formik'; |
| 10 | +import { object, string } from 'yup'; |
| 11 | +import useFlash from '@/plugins/useFlash'; |
| 12 | +import { FlashStore } from '@/state/flashes'; |
| 13 | +import Field from '@/components/elements/Field'; |
13 | 14 |
|
14 | | -const Container = styled.div` |
15 | | - ${breakpoint('sm')` |
16 | | - ${tw`w-4/5 mx-auto`} |
17 | | - `}; |
| 15 | +interface Values { |
| 16 | + code: string; |
| 17 | +} |
18 | 18 |
|
19 | | - ${breakpoint('md')` |
20 | | - ${tw`p-10`} |
21 | | - `}; |
| 19 | +type OwnProps = RouteComponentProps<{}, StaticContext, { token?: string }> |
22 | 20 |
|
23 | | - ${breakpoint('lg')` |
24 | | - ${tw`w-3/5`} |
25 | | - `}; |
| 21 | +type Props = OwnProps & { |
| 22 | + addError: ActionCreator<FlashStore['addError']['payload']>; |
| 23 | + clearFlashes: ActionCreator<FlashStore['clearFlashes']['payload']>; |
| 24 | +} |
26 | 25 |
|
27 | | - ${breakpoint('xl')` |
28 | | - ${tw`w-full`} |
29 | | - max-width: 660px; |
30 | | - `}; |
31 | | -`; |
| 26 | +const LoginCheckpointContainer = () => { |
| 27 | + const { isSubmitting } = useFormikContext<Values>(); |
32 | 28 |
|
33 | | -export default ({ history, location: { state } }: RouteComponentProps<{}, StaticContext, { token?: string }>) => { |
34 | | - const [ code, setCode ] = useState(''); |
35 | | - const [ isLoading, setIsLoading ] = useState(false); |
36 | | - |
37 | | - const { clearFlashes, addFlash } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); |
38 | | - |
39 | | - if (!state || !state.token) { |
40 | | - history.replace('/auth/login'); |
41 | | - |
42 | | - return null; |
43 | | - } |
44 | | - |
45 | | - const onChangeHandler = (e: React.ChangeEvent<HTMLInputElement>) => { |
46 | | - if (e.target.value.length <= 6) { |
47 | | - setCode(e.target.value); |
48 | | - } |
49 | | - }; |
50 | | - |
51 | | - const submit = (e: React.FormEvent<HTMLFormElement>) => { |
52 | | - e.preventDefault(); |
| 29 | + return ( |
| 30 | + <LoginFormContainer |
| 31 | + title={'Device Checkpoint'} |
| 32 | + className={'w-full flex'} |
| 33 | + > |
| 34 | + <div className={'mt-6'}> |
| 35 | + <Field |
| 36 | + light={true} |
| 37 | + name={'code'} |
| 38 | + title={'Authentication Code'} |
| 39 | + description={'Enter the two-factor token generated by your device.'} |
| 40 | + type={'number'} |
| 41 | + autoFocus={true} |
| 42 | + /> |
| 43 | + </div> |
| 44 | + <div className={'mt-6'}> |
| 45 | + <button |
| 46 | + type={'submit'} |
| 47 | + className={'btn btn-primary btn-jumbo'} |
| 48 | + disabled={isSubmitting} |
| 49 | + > |
| 50 | + {isSubmitting ? |
| 51 | + <Spinner size={'tiny'} className={'mx-auto'}/> |
| 52 | + : |
| 53 | + 'Continue' |
| 54 | + } |
| 55 | + </button> |
| 56 | + </div> |
| 57 | + <div className={'mt-6 text-center'}> |
| 58 | + <Link |
| 59 | + to={'/auth/login'} |
| 60 | + className={'text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700'} |
| 61 | + > |
| 62 | + Return to Login |
| 63 | + </Link> |
| 64 | + </div> |
| 65 | + </LoginFormContainer> |
| 66 | + ); |
| 67 | +}; |
53 | 68 |
|
54 | | - setIsLoading(true); |
| 69 | +const EnhancedForm = withFormik<Props, Values>({ |
| 70 | + handleSubmit: ({ code }, { setSubmitting, props: { addError, clearFlashes, location } }) => { |
55 | 71 | clearFlashes(); |
56 | | - |
57 | | - loginCheckpoint(state.token!, code) |
| 72 | + console.log(location.state.token, code); |
| 73 | + loginCheckpoint(location.state?.token || '', code) |
58 | 74 | .then(response => { |
59 | 75 | if (response.complete) { |
60 | 76 | // @ts-ignore |
61 | 77 | window.location = response.intended || '/'; |
| 78 | + return; |
62 | 79 | } |
| 80 | +å |
| 81 | + setSubmitting(false); |
63 | 82 | }) |
64 | 83 | .catch(error => { |
65 | 84 | console.error(error); |
66 | | - addFlash({ type: 'error', title: 'Error', message: httpErrorToHuman(error) }); |
67 | | - setIsLoading(false); |
| 85 | + setSubmitting(false); |
| 86 | + addError({ message: httpErrorToHuman(error) }); |
68 | 87 | }); |
69 | | - }; |
| 88 | + }, |
70 | 89 |
|
71 | | - return ( |
72 | | - <React.Fragment> |
73 | | - <h2 className={'text-center text-neutral-100 font-medium py-4'}> |
74 | | - Device Checkpoint |
75 | | - </h2> |
76 | | - <Container> |
77 | | - <FlashMessageRender/> |
78 | | - <LoginFormContainer onSubmit={submit}> |
79 | | - <div className={'mt-6'}> |
80 | | - <label htmlFor={'authentication_code'}>Authentication Code</label> |
81 | | - <input |
82 | | - id={'authentication_code'} |
83 | | - type={'number'} |
84 | | - autoFocus={true} |
85 | | - className={'input'} |
86 | | - value={code} |
87 | | - onChange={onChangeHandler} |
88 | | - /> |
89 | | - </div> |
90 | | - <div className={'mt-6'}> |
91 | | - <button |
92 | | - type={'submit'} |
93 | | - className={'btn btn-primary btn-jumbo'} |
94 | | - disabled={isLoading || code.length !== 6} |
95 | | - > |
96 | | - {isLoading ? |
97 | | - <Spinner size={'tiny'} className={'mx-auto'}/> |
98 | | - : |
99 | | - 'Continue' |
100 | | - } |
101 | | - </button> |
102 | | - </div> |
103 | | - <div className={'mt-6 text-center'}> |
104 | | - <Link |
105 | | - to={'/auth/login'} |
106 | | - className={'text-xs text-neutral-500 tracking-wide uppercase no-underline hover:text-neutral-700'} |
107 | | - > |
108 | | - Return to Login |
109 | | - </Link> |
110 | | - </div> |
111 | | - </LoginFormContainer> |
112 | | - </Container> |
113 | | - </React.Fragment> |
114 | | - ); |
| 90 | + mapPropsToValues: () => ({ |
| 91 | + code: '', |
| 92 | + }), |
| 93 | + |
| 94 | + validationSchema: object().shape({ |
| 95 | + code: string().required('An authentication code must be provided.') |
| 96 | + .length(6, 'Authentication code must be 6 digits in length.'), |
| 97 | + }), |
| 98 | +})(LoginCheckpointContainer); |
| 99 | + |
| 100 | +export default ({ history, location, ...props }: OwnProps) => { |
| 101 | + const { addError, clearFlashes } = useFlash(); |
| 102 | + |
| 103 | + if (!location.state?.token) { |
| 104 | + history.replace('/auth/login'); |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return <EnhancedForm |
| 110 | + addError={addError} |
| 111 | + clearFlashes={clearFlashes} |
| 112 | + history={history} |
| 113 | + location={location} |
| 114 | + {...props} |
| 115 | + />; |
115 | 116 | }; |
0 commit comments