Debian Codegraph @

examples/react-spa/frontend/src/mfa/ReauthenticateWebAuthn.js

raw
1 import { useState } from 'react'
2 import { Flows, getWebAuthnRequestOptionsForReauthentication, reauthenticateUsingWebAuthn } from '../lib/allauth'
3 import ReauthenticateFlow from '../account/ReauthenticateFlow'
4 import Button from '../components/Button'
5 import {
6 parseRequestOptionsFromJSON,
7 get
8 } from '@github/webauthn-json/browser-ponyfill'
9
10 export default function ReauthenticateWebAuthn () {
11 const [response, setResponse] = useState({ fetching: false, content: null })
12
13 async function submit () {
14 setResponse({ ...response, fetching: true })
15 try {
16 const optResp = await getWebAuthnRequestOptionsForReauthentication()
17 const jsonOptions = optResp.data.request_options
18 const options = parseRequestOptionsFromJSON(jsonOptions)
19 const credential = await get(options)
20 const reauthResp = await reauthenticateUsingWebAuthn(credential)
21 setResponse((r) => { return { ...r, content: reauthResp } })
22 } catch (e) {
23 console.error(e)
24 window.alert(e)
25 }
26 setResponse((r) => { return { ...r, fetching: false } })
27 }
28
29 return (
30 <ReauthenticateFlow flow={Flows.MFA_REAUTHENTICATE}>
31
32 <Button disabled={response.fetching} onClick={() => submit()}>Use security key</Button>
33 </ReauthenticateFlow>
34 )
35 }