Debian Codegraph @

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

raw
1 import { useState } from 'react'
2 import FormErrors from '../components/FormErrors'
3 import { Flows } from '../lib/allauth'
4 import { Navigate } from 'react-router-dom'
5 import Button from '../components/Button'
6 import { useAuthStatus } from '../auth'
7 import * as allauth from '../lib/allauth'
8 import {
9 create,
10 parseCreationOptionsFromJSON
11 } from '@github/webauthn-json/browser-ponyfill'
12
13 export default function CreateSignupPasskey () {
14 const [, authInfo] = useAuthStatus()
15 const [name, setName] = useState('')
16 const [response, setResponse] = useState({ fetching: false, content: null })
17
18 async function submit () {
19 setResponse({ ...response, fetching: true })
20 try {
21 const optResp = await allauth.getWebAuthnCreateOptionsAtSignup(true)
22 if (optResp.status === 200) {
23 const jsonOptions = optResp.data.creation_options
24 const options = parseCreationOptionsFromJSON(jsonOptions)
25 const credential = await create(options)
26 const signupResp = await allauth.signupWebAuthnCredential(name, credential)
27 setResponse((r) => { return { ...r, content: signupResp } })
28 } else {
29 setResponse((r) => { return { ...r, content: optResp } })
30 }
31 } catch (e) {
32 console.error(e)
33 window.alert(e)
34 }
35 setResponse((r) => { return { ...r, fetching: false } })
36 }
37
38 if (response.content?.status === 409 || authInfo.pendingFlow?.id !== Flows.MFA_WEBAUTHN_SIGNUP) {
39 return <Navigate to='/account/signup/passkey' />
40 }
41 return (
42 <div>
43 <h1>Create Passkey</h1>
44 <p>
45 You are about to create a passkey for your account. As you can add additional keys later on, you can use a descriptive name to tell the keys apart.
46 </p>
47
48 <FormErrors errors={response.content?.errors} />
49
50 <div><label>Name <input value={name} onChange={(e) => setName(e.target.value)} type='text' required /></label>
51 <FormErrors param='code' errors={response.content?.errors} />
52 </div>
53 <Button disabled={response.fetching} onClick={() => submit()}>Create</Button>
54 </div>
55 )
56 }