| 1 |
import { useState } from 'react' |
| 2 |
import { Navigate, useLoaderData } from 'react-router-dom' |
| 3 |
import FormErrors from '../components/FormErrors' |
| 4 |
import Button from '../components/Button' |
| 5 |
|
| 6 |
import * as allauth from '../lib/allauth' |
| 7 |
|
| 8 |
export async function loader ({ params }) { |
| 9 |
const resp = await allauth.getRecoveryCodes() |
| 10 |
return { recoveryCodes: resp } |
| 11 |
} |
| 12 |
|
| 13 |
export default function GenerateRecoveryCodes () { |
| 14 |
const { recoveryCodes } = useLoaderData() |
| 15 |
const [response, setResponse] = useState({ fetching: false, content: null }) |
| 16 |
|
| 17 |
function submit () { |
| 18 |
setResponse({ ...response, fetching: true }) |
| 19 |
allauth.generateRecoveryCodes().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 (response.content?.status === 200) { |
| 29 |
return <Navigate to='/account/2fa/recovery-codes' /> |
| 30 |
} |
| 31 |
|
| 32 |
const hasCodes = recoveryCodes.status === 200 && recoveryCodes.data.unused_code_count > 0 |
| 33 |
return ( |
| 34 |
<section> |
| 35 |
<h1>Recovery Codes</h1> |
| 36 |
|
| 37 |
<p>You are about to generate a new set of recovery codes for your account. |
| 38 |
{hasCodes ? 'This action will invalidate your existing codes.' : ''} Are you sure? |
| 39 |
</p> |
| 40 |
|
| 41 |
<FormErrors errors={response.content?.errors} /> |
| 42 |
|
| 43 |
<Button onClick={() => submit()}>Generate</Button> |
| 44 |
|
| 45 |
</section> |
| 46 |
) |
| 47 |
} |