All files / src/screens PasskeyManage.tsx

68.42% Statements 39/57
50% Branches 17/34
84.61% Functions 11/13
65.38% Lines 34/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                    18x 10x                                               7x                                   9x                                                   9x                             2x                     16x 16x 16x 16x 16x   16x 1x 1x 1x       1x       16x 8x 8x 8x 8x 6x   1x   7x     8x                                                                         2x 1x   1x 1x 1x 1x           16x 8x             8x                      
import { useState, useEffect, useCallback } from 'react'
import { useNavigate } from 'react-router-dom'
import { api } from '../api/client'
import type { PasskeyCredential } from '../api/types'
import {
  prepareRegistrationOptions,
  serializeRegistrationCredential,
} from '../lib/webauthn'
 
function formatDate(dateStr: string | null): string {
  if (!dateStr) return 'Never'
  return new Date(dateStr).toLocaleDateString(undefined, {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
  })
}
 
interface PasskeyCardProps {
  credential: PasskeyCredential
  onDelete: (id: number) => void
}
 
interface PasskeyListProps {
  credentials: PasskeyCredential[]
  error: string
  adding: boolean
  onAdd: () => void
  onDelete: (id: number) => void
  onBack: () => void
}
 
function PasskeyList({ credentials, error, adding, onAdd, onDelete, onBack }: PasskeyListProps) {
  return (
    <div className="flex min-h-screen items-center justify-center bg-background p-4">
      <div className="w-full max-w-md space-y-6">
        <div className="text-center">
          <h1 className="text-2xl font-bold text-foreground">Manage Passkeys</h1>
          <p className="mt-1 text-sm text-muted-foreground">
            {credentials.length} passkey{credentials.length !== 1 ? 's' : ''} registered
          </p>
        </div>
 
        {error && (
          <div role="alert" className="rounded-md bg-red-50 p-3 text-sm text-red-800 dark:bg-red-900/20 dark:text-red-300">
            {error}
          </div>
        )}
 
        <div className="space-y-3">
          {credentials.map((cred) => (
            <PasskeyCard key={cred.id} credential={cred} onDelete={onDelete} />
          ))}
        </div>
 
        <button
          onClick={onAdd}
          disabled={adding}
          className="w-full rounded-md bg-primary px-4 py-2 text-sm font-medium text-primary-foreground shadow-sm hover:bg-primary/90 disabled:opacity-50"
        >
          {adding ? 'Adding passkey...' : 'Add Passkey'}
        </button>
 
        <div className="text-center">
          <button
            onClick={onBack}
            className="text-sm text-muted-foreground hover:text-foreground"
          >
            Go back
          </button>
        </div>
      </div>
    </div>
  )
}
 
function PasskeyCard({ credential, onDelete }: PasskeyCardProps) {
  return (
    <div className="flex items-center justify-between rounded-lg border border-input bg-background p-4">
      <div>
        <div className="text-sm font-medium text-foreground">
          Passkey #{credential.id}
        </div>
        <div className="text-xs text-muted-foreground">
          Added {formatDate(credential.created_at)}
        </div>
        <div className="text-xs text-muted-foreground">
          Last used: {formatDate(credential.last_used_at)}
        </div>
      </div>
      {credential.is_deletable && (
        <button
          onClick={() => onDelete(credential.id)}
          className="rounded-md px-3 py-1 text-sm text-red-600 hover:bg-red-50 dark:text-red-400 dark:hover:bg-red-900/20"
        >
          Delete
        </button>
      )}
    </div>
  )
}
 
export default function PasskeyManage() {
  const navigate = useNavigate()
  const [credentials, setCredentials] = useState<PasskeyCredential[]>([])
  const [loading, setLoading] = useState(true)
  const [error, setError] = useState('')
  const [adding, setAdding] = useState(false)
 
  const loadCredentials = useCallback(async () => {
    try {
      const data = await api.passkey.listCredentials()
      setCredentials(data.credentials)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to load passkeys')
    } finally {
      setLoading(false)
    }
  }, [])
 
  useEffect(() => {
    let cancelled = false
    ;(async () => {
      try {
        const data = await api.passkey.listCredentials()
        Eif (!cancelled) setCredentials(data.credentials)
      } catch (err) {
        Eif (!cancelled) setError(err instanceof Error ? err.message : 'Failed to load passkeys')
      } finally {
        Eif (!cancelled) setLoading(false)
      }
    })()
    return () => { cancelled = true }
  }, [])
 
  async function handleAdd() {
    setError('')
    setAdding(true)
 
    try {
      const options = await api.passkey.addCredentialOptions()
      const publicKeyOptions = prepareRegistrationOptions(options)
 
      const credential = await navigator.credentials.create({
        publicKey: publicKeyOptions,
      })
 
      if (!credential) {
        setError('Adding passkey was cancelled.')
        return
      }
 
      await api.passkey.addCredentialVerify(
        serializeRegistrationCredential(credential as PublicKeyCredential)
      )
 
      await loadCredentials()
    } catch (err) {
      if (err instanceof DOMException && err.name === 'NotAllowedError') {
        setError('Adding passkey was cancelled.')
      } else {
        setError(err instanceof Error ? err.message : 'Failed to add passkey')
      }
    } finally {
      setAdding(false)
    }
  }
 
  async function handleDelete(credentialId: number) {
    if (!window.confirm('Are you sure you want to delete this passkey? This cannot be undone.')) {
      return
    }
    setError('')
    try {
      await api.passkey.deleteCredential(credentialId)
      await loadCredentials()
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Failed to delete passkey')
    }
  }
 
  if (loading) {
    return (
      <div className="flex min-h-screen items-center justify-center bg-background">
        <div className="text-muted-foreground">Loading...</div>
      </div>
    )
  }
 
  return (
    <PasskeyList
      credentials={credentials}
      error={error}
      adding={adding}
      onAdd={handleAdd}
      onDelete={handleDelete}
      onBack={() => navigate(-1)}
    />
  )
}
 
← Back to Dashboard