| 1 |
import { useState } from 'react' |
| 2 |
import FormErrors from '../components/FormErrors' |
| 3 |
import * as allauth from '../lib/allauth' |
| 4 |
import Button from '../components/Button' |
| 5 |
import { useAuthInfo } from '../auth/hooks' |
| 6 |
import { Navigate } from 'react-router-dom' |
| 7 |
import AuthenticateFlow from './AuthenticateFlow' |
| 8 |
|
| 9 |
export default function AuthenticateCode (props) { |
| 10 |
const [code, setCode] = useState('') |
| 11 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 12 |
const authInfo = useAuthInfo() |
| 13 |
|
| 14 |
if (authInfo?.pendingFlow?.id !== allauth.Flows.MFA_AUTHENTICATE) { |
| 15 |
return <Navigate to='/' /> |
| 16 |
} |
| 17 |
|
| 18 |
function submit () { |
| 19 |
setResponse({ ...response, fetching: true }) |
| 20 |
allauth.mfaAuthenticate(code).then((content) => { |
| 21 |
setResponse((r) => { return { ...r, content } }) |
| 22 |
}).catch((e) => { |
| 23 |
console.error(e) |
| 24 |
window.alert(e) |
| 25 |
}).then(() => { |
| 26 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 27 |
}) |
| 28 |
} |
| 29 |
return ( |
| 30 |
<AuthenticateFlow authenticatorType={props.authenticatorType}> |
| 31 |
{props.children} |
| 32 |
<label> |
| 33 |
Code: |
| 34 |
<input type='text' value={code} onChange={(e) => setCode(e.target.value)} /> |
| 35 |
</label> |
| 36 |
<FormErrors param='code' errors={response.content?.errors} /> |
| 37 |
<Button onClick={() => submit()}>Sign In</Button> |
| 38 |
</AuthenticateFlow> |
| 39 |
) |
| 40 |
} |