|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Button } from "@starter-saas/ui/components/button"; |
| 4 | +import { |
| 5 | + Card, |
| 6 | + CardContent, |
| 7 | + CardDescription, |
| 8 | + CardHeader, |
| 9 | + CardTitle, |
| 10 | +} from "@starter-saas/ui/components/card"; |
| 11 | +import { EmptyState } from "@starter-saas/ui/components/empty-state"; |
| 12 | +import { Skeleton } from "@starter-saas/ui/components/skeleton"; |
| 13 | +import { Fingerprint } from "lucide-react"; |
| 14 | +import { useEffect, useState } from "react"; |
| 15 | +import { toast } from "sonner"; |
| 16 | +import { authClient } from "@/lib/auth-client"; |
| 17 | + |
| 18 | +type PasskeyRow = { |
| 19 | + id: string; |
| 20 | + name?: string | null; |
| 21 | + deviceType?: string | null; |
| 22 | + createdAt?: string | Date | null; |
| 23 | +}; |
| 24 | + |
| 25 | +export function PasskeySection() { |
| 26 | + const [passkeys, setPasskeys] = useState<PasskeyRow[] | null>(null); |
| 27 | + const [enrolling, setEnrolling] = useState(false); |
| 28 | + |
| 29 | + const refresh = async () => { |
| 30 | + try { |
| 31 | + // Better Auth's passkey plugin exposes listUserPasskeys on the client. |
| 32 | + // The shape isn't strongly typed for arbitrary plugins so we coerce. |
| 33 | + const res = await ( |
| 34 | + authClient as unknown as { |
| 35 | + listPasskeys: () => Promise<{ data?: PasskeyRow[] | null }>; |
| 36 | + } |
| 37 | + ).listPasskeys(); |
| 38 | + setPasskeys(res?.data ?? []); |
| 39 | + } catch { |
| 40 | + setPasskeys([]); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + useEffect(() => { |
| 45 | + refresh(); |
| 46 | + }, []); |
| 47 | + |
| 48 | + const enroll = async () => { |
| 49 | + setEnrolling(true); |
| 50 | + const id = toast.loading("Tap your security key or biometric sensor…"); |
| 51 | + try { |
| 52 | + const result = await authClient.passkey.addPasskey(); |
| 53 | + if (result?.error) { |
| 54 | + toast.error("Couldn't register passkey", { |
| 55 | + id, |
| 56 | + description: result.error.message, |
| 57 | + }); |
| 58 | + return; |
| 59 | + } |
| 60 | + toast.success("Passkey registered", { id }); |
| 61 | + await refresh(); |
| 62 | + } catch (err) { |
| 63 | + toast.error("Couldn't register passkey", { |
| 64 | + id, |
| 65 | + description: err instanceof Error ? err.message : "?", |
| 66 | + }); |
| 67 | + } finally { |
| 68 | + setEnrolling(false); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + const remove = async (passkeyId: string) => { |
| 73 | + try { |
| 74 | + await ( |
| 75 | + authClient as unknown as { |
| 76 | + deletePasskey: (args: { id: string }) => Promise<unknown>; |
| 77 | + } |
| 78 | + ).deletePasskey({ id: passkeyId }); |
| 79 | + toast.success("Passkey removed"); |
| 80 | + await refresh(); |
| 81 | + } catch (err) { |
| 82 | + toast.error("Couldn't remove passkey", { |
| 83 | + description: err instanceof Error ? err.message : "?", |
| 84 | + }); |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + return ( |
| 89 | + <Card> |
| 90 | + <CardHeader> |
| 91 | + <CardTitle className="flex items-center gap-2"> |
| 92 | + <Fingerprint className="h-5 w-5" /> |
| 93 | + Passkeys |
| 94 | + </CardTitle> |
| 95 | + <CardDescription> |
| 96 | + Sign in with Face ID, Touch ID, Windows Hello, or a hardware key — |
| 97 | + phishing-resistant, no password to forget. |
| 98 | + </CardDescription> |
| 99 | + </CardHeader> |
| 100 | + <CardContent className="space-y-4"> |
| 101 | + {passkeys === null ? ( |
| 102 | + <div className="space-y-2"> |
| 103 | + <Skeleton className="h-10 w-full" /> |
| 104 | + <Skeleton className="h-10 w-full" /> |
| 105 | + </div> |
| 106 | + ) : passkeys.length === 0 ? ( |
| 107 | + <EmptyState |
| 108 | + illustration="arc" |
| 109 | + title="No passkeys yet" |
| 110 | + description="Register one to skip passwords on this device next time you sign in." |
| 111 | + className="border-0 bg-transparent py-6" |
| 112 | + /> |
| 113 | + ) : ( |
| 114 | + <ul className="divide-y"> |
| 115 | + {passkeys.map((pk) => ( |
| 116 | + <li key={pk.id} className="flex items-center gap-3 py-3"> |
| 117 | + <div className="flex h-9 w-9 items-center justify-center rounded-md bg-muted"> |
| 118 | + <Fingerprint className="h-4 w-4" /> |
| 119 | + </div> |
| 120 | + <div className="flex-1"> |
| 121 | + <p className="font-medium text-sm"> |
| 122 | + {pk.name?.trim() || "Unnamed passkey"} |
| 123 | + </p> |
| 124 | + <p className="text-muted-foreground text-xs"> |
| 125 | + {pk.deviceType ?? "—"} |
| 126 | + {pk.createdAt |
| 127 | + ? ` · ${new Date(pk.createdAt).toLocaleDateString()}` |
| 128 | + : ""} |
| 129 | + </p> |
| 130 | + </div> |
| 131 | + <Button variant="ghost" size="sm" onClick={() => remove(pk.id)}> |
| 132 | + Remove |
| 133 | + </Button> |
| 134 | + </li> |
| 135 | + ))} |
| 136 | + </ul> |
| 137 | + )} |
| 138 | + <Button onClick={enroll} disabled={enrolling}> |
| 139 | + {enrolling ? "Waiting for device…" : "Register a passkey"} |
| 140 | + </Button> |
| 141 | + </CardContent> |
| 142 | + </Card> |
| 143 | + ); |
| 144 | +} |
0 commit comments