| 105 | .replaceAll('/', '_') |
| 106 | |
| 107 | async function getPublicKey(username: string, create = false) { |
| 108 | const r = await fetch( |
| 109 | `${API_HOST}/${create ? 'register' : 'auth'}/${encodeURIComponent( |
| 110 | username |
| 111 | )}`, |
| 112 | { |
| 113 | credentials: 'same-origin' |
| 114 | } |
| 115 | ) |
| 116 | |
| 117 | if (r.status !== 200) { |
| 118 | throw new Error(`Unexpected response ${r.status}: ${await r.text()}`) |
| 119 | } |
| 120 | if (create) { |
| 121 | return (await r.json()) as PublicKeyCredentialCreationOptions |
| 122 | } |
| 123 | return (await r.json()) as PublicKeyCredentialRequestOptions |
| 124 | } |
| 125 | |
| 126 | interface AuthResponse { |
| 127 | attestationObject?: ArrayBuffer |