Debian Codegraph @

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

raw
1 import Button from '../components/Button'
2 import { useState } from 'react'
3 import { getWebAuthnRequestOptionsForLogin, loginUsingWebAuthn } from '../lib/allauth'
4 import {
5 parseRequestOptionsFromJSON,
6 get
7 } from '@github/webauthn-json/browser-ponyfill'
8
9 export default function WebAuthnLoginButton (props) {
10 const [response, setResponse] = useState({ fetching: false, content: null })
11
12 async function submit (e) {
13 e.preventDefault()
14 setResponse({ ...response, fetching: true })
15 try {
16 const optResp = await getWebAuthnRequestOptionsForLogin()
17 const jsonOptions = optResp.data.request_options
18 const options = parseRequestOptionsFromJSON(jsonOptions)
19 const credential = await get(options)
20 const loginResp = await loginUsingWebAuthn(credential)
21 setResponse((r) => { return { ...r, content: loginResp } })
22 } catch (e) {
23 console.error(e)
24 window.alert(e)
25 }
26 setResponse((r) => { return { ...r, fetching: false } })
27 }
28 return <Button onClick={submit}>{props.children}</Button>
29 }