import { useState } from 'react' import { Link, useLoaderData, Navigate } from 'react-router-dom' import Button from '../components/Button' import * as allauth from '../lib/allauth' export async function loader ({ params }) { const resp = await allauth.getAuthenticators() return { authenticators: resp.data } } function Authenticator (props) { const [name, setName] = useState(props.authenticator.name) const { authenticator } = props function onSubmit (e) { e.preventDefault() props.onSave(name) } return ( {props.editting ?
setName(e.target.value)} value={name} type='text' />
: {authenticator.name} } {typeof authenticator.is_passwordless === 'undefined' ? 'Type unspecified' : (authenticator.is_passwordless ? 'Passkey' : 'Security key')} {new Date(authenticator.created_at * 1000).toLocaleString()} {authenticator.last_used_at ? new Date(authenticator.last_used_at * 1000).toLocaleString() : 'Unused'} ) } export default function ListWebAuthn (props) { const { authenticators } = useLoaderData() const [editId, setEditId] = useState(null) const [keys, setKeys] = useState(() => authenticators.filter(authenticator => authenticator.type === allauth.AuthenticatorType.WEBAUTHN)) const [response, setResponse] = useState({ fetching: false, content: null }) async function optimisticSetKeys (newKeys, op) { setResponse({ ...response, fetching: true }) const oldKeys = keys setEditId(null) setKeys(newKeys) try { const ok = await op() if (!ok) { setKeys(oldKeys) } } catch (e) { setKeys(oldKeys) console.error(e) window.alert(e) } setResponse((r) => { return { ...r, fetching: false } }) } async function deleteKey (key) { const newKeys = keys.filter((k) => k.id !== key.id) await optimisticSetKeys(newKeys, async () => { const resp = await allauth.deleteWebAuthnCredential([key.id]) return (resp.status === 200) }) } async function onSave (key, name) { const newKeys = keys.filter((k) => k.id !== key.id) newKeys.push({ ...key, name }) await optimisticSetKeys(newKeys, async () => { const resp = await allauth.updateWebAuthnCredential(key.id, { name }) return (resp.status === 200) }) } if (!keys.length && !response.fetching) { return } return (

Security Keys

{keys.map(key => { return ( setEditId(null)} onSave={(name) => onSave(key, name)} onEdit={() => setEditId(key.id)} onDelete={() => deleteKey(key)} /> ) })}
Name Type Created At Last Used At Actions
Add
) }