|
| 1 | +import { Fingerprint, Loader2, Plus, Trash2 } from "lucide-react"; |
| 2 | +import { useState } from "react"; |
| 3 | +import { toast } from "sonner"; |
| 4 | +import { DateTooltip } from "@/components/shared/date-tooltip"; |
| 5 | +import { DialogAction } from "@/components/shared/dialog-action"; |
| 6 | +import { Badge } from "@/components/ui/badge"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { |
| 9 | + Dialog, |
| 10 | + DialogContent, |
| 11 | + DialogDescription, |
| 12 | + DialogHeader, |
| 13 | + DialogTitle, |
| 14 | + DialogTrigger, |
| 15 | +} from "@/components/ui/dialog"; |
| 16 | +import { Input } from "@/components/ui/input"; |
| 17 | +import { Label } from "@/components/ui/label"; |
| 18 | +import { authClient } from "@/lib/auth-client"; |
| 19 | + |
| 20 | +export const ManagePasskeys = () => { |
| 21 | + const [isDialogOpen, setIsDialogOpen] = useState(false); |
| 22 | + const { data: passkeys, isPending, refetch } = authClient.useListPasskeys(); |
| 23 | + const [name, setName] = useState(""); |
| 24 | + const [isAdding, setIsAdding] = useState(false); |
| 25 | + const [deletingId, setDeletingId] = useState<string | null>(null); |
| 26 | + |
| 27 | + const handleAddPasskey = async (e: React.FormEvent) => { |
| 28 | + e.preventDefault(); |
| 29 | + setIsAdding(true); |
| 30 | + try { |
| 31 | + const result = await authClient.passkey.addPasskey({ |
| 32 | + name: name.trim() || undefined, |
| 33 | + }); |
| 34 | + |
| 35 | + if (result?.error) { |
| 36 | + toast.error(result.error.message); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + toast.success("Passkey added successfully"); |
| 41 | + setName(""); |
| 42 | + refetch(); |
| 43 | + } catch { |
| 44 | + toast.error("Failed to add passkey"); |
| 45 | + } finally { |
| 46 | + setIsAdding(false); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + const handleDeletePasskey = async (id: string) => { |
| 51 | + setDeletingId(id); |
| 52 | + try { |
| 53 | + const result = await authClient.passkey.deletePasskey({ id }); |
| 54 | + |
| 55 | + if (result?.error) { |
| 56 | + toast.error(result.error.message); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + toast.success("Passkey removed"); |
| 61 | + refetch(); |
| 62 | + } catch { |
| 63 | + toast.error("Failed to remove passkey"); |
| 64 | + } finally { |
| 65 | + setDeletingId(null); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}> |
| 71 | + <DialogTrigger asChild> |
| 72 | + <Button variant="secondary"> |
| 73 | + <Fingerprint className="size-4 text-muted-foreground" /> |
| 74 | + Passkeys |
| 75 | + </Button> |
| 76 | + </DialogTrigger> |
| 77 | + <DialogContent className="sm:max-w-xl"> |
| 78 | + <DialogHeader> |
| 79 | + <DialogTitle>Passkeys</DialogTitle> |
| 80 | + <DialogDescription> |
| 81 | + Sign in without a password using your device's biometrics, security |
| 82 | + key, or password manager. |
| 83 | + </DialogDescription> |
| 84 | + </DialogHeader> |
| 85 | + |
| 86 | + <div className="space-y-4"> |
| 87 | + {isPending ? ( |
| 88 | + <div className="flex flex-row gap-2 items-center justify-center text-sm text-muted-foreground min-h-[10vh]"> |
| 89 | + <span>Loading...</span> |
| 90 | + <Loader2 className="animate-spin size-4" /> |
| 91 | + </div> |
| 92 | + ) : passkeys && passkeys.length > 0 ? ( |
| 93 | + <div className="grid gap-3"> |
| 94 | + {passkeys.map((passkey) => ( |
| 95 | + <div |
| 96 | + key={passkey.id} |
| 97 | + className="flex items-center justify-between gap-2 p-4 border rounded-lg" |
| 98 | + > |
| 99 | + <div className="flex flex-col gap-1 min-w-0"> |
| 100 | + <span className="font-medium flex items-center gap-2"> |
| 101 | + <Fingerprint className="size-4 text-muted-foreground shrink-0" /> |
| 102 | + <span className="truncate"> |
| 103 | + {passkey.name || "Unnamed passkey"} |
| 104 | + </span> |
| 105 | + <Badge variant="outline"> |
| 106 | + {passkey.deviceType === "singleDevice" |
| 107 | + ? "Device" |
| 108 | + : "Synced"} |
| 109 | + </Badge> |
| 110 | + </span> |
| 111 | + {passkey.createdAt && ( |
| 112 | + <DateTooltip |
| 113 | + date={passkey.createdAt.toString()} |
| 114 | + className="text-sm" |
| 115 | + > |
| 116 | + Added |
| 117 | + </DateTooltip> |
| 118 | + )} |
| 119 | + </div> |
| 120 | + <DialogAction |
| 121 | + title="Remove this passkey?" |
| 122 | + description="You will no longer be able to sign in with it. This action cannot be undone." |
| 123 | + onClick={() => handleDeletePasskey(passkey.id)} |
| 124 | + > |
| 125 | + <Button |
| 126 | + variant="ghost" |
| 127 | + size="icon" |
| 128 | + isLoading={deletingId === passkey.id} |
| 129 | + className="text-muted-foreground hover:text-destructive" |
| 130 | + > |
| 131 | + <Trash2 className="size-4" /> |
| 132 | + </Button> |
| 133 | + </DialogAction> |
| 134 | + </div> |
| 135 | + ))} |
| 136 | + </div> |
| 137 | + ) : ( |
| 138 | + <div className="flex flex-col items-center gap-2 py-6 text-sm text-muted-foreground border rounded-lg"> |
| 139 | + <Fingerprint className="size-6" /> |
| 140 | + <span>No passkeys registered yet</span> |
| 141 | + </div> |
| 142 | + )} |
| 143 | + |
| 144 | + <form onSubmit={handleAddPasskey} className="flex flex-col gap-2"> |
| 145 | + <Label htmlFor="passkey-name">Passkey Name</Label> |
| 146 | + <div className="flex gap-2"> |
| 147 | + <Input |
| 148 | + id="passkey-name" |
| 149 | + placeholder="e.g. MacBook Touch ID" |
| 150 | + value={name} |
| 151 | + onChange={(e) => setName(e.target.value)} |
| 152 | + /> |
| 153 | + <Button type="submit" isLoading={isAdding}> |
| 154 | + <Plus className="size-4" /> |
| 155 | + Add Passkey |
| 156 | + </Button> |
| 157 | + </div> |
| 158 | + </form> |
| 159 | + </div> |
| 160 | + </DialogContent> |
| 161 | + </Dialog> |
| 162 | + ); |
| 163 | +}; |
0 commit comments