| 1 |
import { useState } from 'react' |
| 2 |
import * as allauth from '../lib/allauth' |
| 3 |
import { Navigate, useLoaderData } from 'react-router-dom' |
| 4 |
import FormErrors from '../components/FormErrors' |
| 5 |
import Button from '../components/Button' |
| 6 |
|
| 7 |
export async function loader ({ params }) { |
| 8 |
const resp = await allauth.getTOTPAuthenticator() |
| 9 |
return { totp: resp } |
| 10 |
} |
| 11 |
|
| 12 |
export default function ActivateTOTP (props) { |
| 13 |
const { totp } = useLoaderData() |
| 14 |
const [code, setCode] = useState('') |
| 15 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 16 |
|
| 17 |
function submit () { |
| 18 |
setResponse({ ...response, fetching: true }) |
| 19 |
allauth.activateTOTPAuthenticator(code).then((content) => { |
| 20 |
setResponse((r) => { return { ...r, content } }) |
| 21 |
}).catch((e) => { |
| 22 |
console.error(e) |
| 23 |
window.alert(e) |
| 24 |
}).then(() => { |
| 25 |
setResponse((r) => { return { ...r, fetching: false } }) |
| 26 |
}) |
| 27 |
} |
| 28 |
if (totp.status === 200 || response.content?.status === 200) { |
| 29 |
return <Navigate to='/account/2fa' /> |
| 30 |
} |
| 31 |
return ( |
| 32 |
<section> |
| 33 |
<h1>Activate TOTP</h1> |
| 34 |
|
| 35 |
<div> |
| 36 |
<label> |
| 37 |
Authenticator secret: |
| 38 |
<input disabled type='text' value={totp.meta?.secret} /> |
| 39 |
<span>You can store this secret and use it to reinstall your authenticator app at a later time.</span> |
| 40 |
</label> |
| 41 |
</div> |
| 42 |
<div> |
| 43 |
<label> |
| 44 |
Authenticator code: |
| 45 |
<input type='text' value={code} onChange={(e) => setCode(e.target.value)} /> |
| 46 |
</label> |
| 47 |
<FormErrors param='code' errors={response.content?.errors} /> |
| 48 |
<FormErrors errors={totp.errors} /> |
| 49 |
</div> |
| 50 |
<Button onClick={() => submit()}>Activate</Button> |
| 51 |
</section> |
| 52 |
) |
| 53 |
} |