Debian Codegraph @

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

raw
1 import { useState } from 'react'
2 import { Link, useLoaderData, Navigate } from 'react-router-dom'
3 import Button from '../components/Button'
4 import * as allauth from '../lib/allauth'
5
6 export async function loader ({ params }) {
7 const resp = await allauth.getAuthenticators()
8 return { authenticators: resp.data }
9 }
10
11 function Authenticator (props) {
12 const [name, setName] = useState(props.authenticator.name)
13 const { authenticator } = props
14 function onSubmit (e) {
15 e.preventDefault()
16 props.onSave(name)
17 }
18 return (
19 <tr>
20 {props.editting
21 ? <td>
22 <form onSubmit={onSubmit}>
23 <input onChange={(e) => setName(e.target.value)} value={name} type='text' />
24 <Button type='button' onClick={() => props.onCancel()}> Cancel</Button>
25 </form>
26 </td>
27 : <td>{authenticator.name} <Button onClick={() => props.onEdit()}> Edit</Button>
28 </td>}
29 <td>{typeof authenticator.is_passwordless === 'undefined'
30 ? 'Type unspecified'
31 : (authenticator.is_passwordless ? 'Passkey' : 'Security key')}
32 </td>
33 <td>{new Date(authenticator.created_at * 1000).toLocaleString()}</td>
34 <td>{authenticator.last_used_at ? new Date(authenticator.last_used_at * 1000).toLocaleString() : 'Unused'}</td>
35 <td>
36 <Button onClick={() => props.onDelete()}>Delete</Button>
37 </td>
38 </tr>
39 )
40 }
41
42 export default function ListWebAuthn (props) {
43 const { authenticators } = useLoaderData()
44 const [editId, setEditId] = useState(null)
45 const [keys, setKeys] = useState(() => authenticators.filter(authenticator => authenticator.type === allauth.AuthenticatorType.WEBAUTHN))
46 const [response, setResponse] = useState({ fetching: false, content: null })
47
48 async function optimisticSetKeys (newKeys, op) {
49 setResponse({ ...response, fetching: true })
50 const oldKeys = keys
51 setEditId(null)
52 setKeys(newKeys)
53 try {
54 const ok = await op()
55 if (!ok) {
56 setKeys(oldKeys)
57 }
58 } catch (e) {
59 setKeys(oldKeys)
60 console.error(e)
61 window.alert(e)
62 }
63 setResponse((r) => { return { ...r, fetching: false } })
64 }
65
66 async function deleteKey (key) {
67 const newKeys = keys.filter((k) => k.id !== key.id)
68 await optimisticSetKeys(newKeys, async () => {
69 const resp = await allauth.deleteWebAuthnCredential([key.id])
70 return (resp.status === 200)
71 })
72 }
73
74 async function onSave (key, name) {
75 const newKeys = keys.filter((k) => k.id !== key.id)
76 newKeys.push({ ...key, name })
77 await optimisticSetKeys(newKeys, async () => {
78 const resp = await allauth.updateWebAuthnCredential(key.id, { name })
79 return (resp.status === 200)
80 })
81 }
82
83 if (!keys.length && !response.fetching) {
84 return <Navigate to='/account/2fa' />
85 }
86
87 return (
88 <section>
89 <h1>Security Keys</h1>
90
91 <table className='table table-striped'>
92 <thead>
93 <tr>
94 <th>Name</th>
95 <th>Type</th>
96 <th>Created At</th>
97 <th>Last Used At</th>
98 <th>Actions</th>
99 </tr>
100 </thead>
101 <tbody>
102 {keys.map(key => {
103 return (
104 <Authenticator
105 key={key.id}
106 editting={key.id === editId} authenticator={key} onCancel={() => setEditId(null)}
107 onSave={(name) => onSave(key, name)}
108 onEdit={() => setEditId(key.id)} onDelete={() => deleteKey(key)}
109 />
110 )
111 })}
112 </tbody>
113 </table>
114 <Link to='/account/2fa/webauthn/add'>Add</Link>
115 </section>
116 )
117 }