Debian Codegraph @

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

raw
1 import { useState } from 'react'
2 import { AuthenticatorType, getWebAuthnRequestOptionsForAuthentication, authenticateUsingWebAuthn } from '../lib/allauth'
3 import Button from '../components/Button'
4 import {
5 parseRequestOptionsFromJSON,
6 get
7 } from '@github/webauthn-json/browser-ponyfill'
8 import AuthenticateFlow from './AuthenticateFlow'
9
10 export default function AuthenticateWebAuthn (props) {
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 getWebAuthnRequestOptionsForAuthentication()
17 const jsonOptions = optResp.data.request_options
18 const options = parseRequestOptionsFromJSON(jsonOptions)
19 const credential = await get(options)
20 const reauthResp = await authenticateUsingWebAuthn(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 <AuthenticateFlow authenticatorType={AuthenticatorType.WEBAUTHN}>
31 <Button disabled={response.fetching} onClick={() => submit()}>Use security key</Button>
32 </AuthenticateFlow>
33 )
34 }