Skip to content

Commit 7b0bcee

Browse files
committed
feat(auth): add passkey support via better-auth
1 parent 11e93dd commit 7b0bcee

13 files changed

Lines changed: 9429 additions & 1 deletion

File tree

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
};

‎apps/dokploy/components/dashboard/settings/profile/profile-form.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { generateSHA256Hash, getFallbackAvatarInitials } from "@/lib/utils";
3131
import { api } from "@/utils/api";
3232
import { Configure2FA } from "./configure-2fa";
3333
import { Enable2FA } from "./enable-2fa";
34+
import { ManagePasskeys } from "./manage-passkeys";
3435

3536
const profileSchema = z.object({
3637
email: z
@@ -162,7 +163,10 @@ export const ProfileForm = () => {
162163
</CardDescription>
163164
</div>
164165

165-
{!data?.user.twoFactorEnabled ? <Enable2FA /> : <Configure2FA />}
166+
<div className="flex flex-row gap-2 flex-wrap">
167+
<ManagePasskeys />
168+
{!data?.user.twoFactorEnabled ? <Enable2FA /> : <Configure2FA />}
169+
</div>
166170
</CardHeader>
167171

168172
<CardContent className="space-y-2 py-8 border-t">
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CREATE TABLE "passkey" (
2+
"id" text PRIMARY KEY NOT NULL,
3+
"name" text,
4+
"public_key" text NOT NULL,
5+
"user_id" text NOT NULL,
6+
"credential_id" text NOT NULL,
7+
"counter" integer NOT NULL,
8+
"device_type" text NOT NULL,
9+
"backed_up" boolean NOT NULL,
10+
"transports" text,
11+
"created_at" timestamp,
12+
"aaguid" text
13+
);
14+
--> statement-breakpoint
15+
ALTER TABLE "passkey" ADD CONSTRAINT "passkey_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
16+
CREATE INDEX "passkey_userId_idx" ON "passkey" USING btree ("user_id");--> statement-breakpoint
17+
CREATE INDEX "passkey_credentialID_idx" ON "passkey" USING btree ("credential_id");

0 commit comments

Comments
 (0)