better-auth-ui 3.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/dist/auth-hooks-IOEvlYYv.d.cts +6966 -0
  4. package/dist/auth-hooks-IOEvlYYv.d.ts +6966 -0
  5. package/dist/auth-mutators-DdqOmQ32.d.cts +29 -0
  6. package/dist/auth-mutators-DdqOmQ32.d.ts +29 -0
  7. package/dist/auth-ui-provider-BsH3xJDw.d.ts +697 -0
  8. package/dist/auth-ui-provider-DhZfncd3.d.cts +697 -0
  9. package/dist/chunk-BDFQSFBU.js +750 -0
  10. package/dist/chunk-CRAHKL2C.cjs +801 -0
  11. package/dist/chunk-MJPOA6PK.js +801 -0
  12. package/dist/chunk-SV64DXGW.cjs +750 -0
  13. package/dist/index.cjs +12618 -0
  14. package/dist/index.d.cts +771 -0
  15. package/dist/index.d.ts +771 -0
  16. package/dist/index.js +12618 -0
  17. package/dist/instantdb.cjs +189 -0
  18. package/dist/instantdb.d.cts +36 -0
  19. package/dist/instantdb.d.ts +36 -0
  20. package/dist/instantdb.js +189 -0
  21. package/dist/metafile-cjs.json +1 -0
  22. package/dist/metafile-esm.json +1 -0
  23. package/dist/server.cjs +194 -0
  24. package/dist/server.d.cts +35 -0
  25. package/dist/server.d.ts +35 -0
  26. package/dist/server.js +194 -0
  27. package/dist/style.css +1 -0
  28. package/dist/tanstack.cjs +153 -0
  29. package/dist/tanstack.d.cts +18 -0
  30. package/dist/tanstack.d.ts +18 -0
  31. package/dist/tanstack.js +153 -0
  32. package/dist/triplit.cjs +201 -0
  33. package/dist/triplit.d.cts +31 -0
  34. package/dist/triplit.d.ts +31 -0
  35. package/dist/triplit.js +201 -0
  36. package/dist/utils-C5R37WDe.d.cts +3 -0
  37. package/dist/utils-C5R37WDe.d.ts +3 -0
  38. package/dist/view-paths-CHSJf5dv.d.cts +645 -0
  39. package/dist/view-paths-CHSJf5dv.d.ts +645 -0
  40. package/package.json +156 -0
  41. package/src/components/account/account-view.tsx +220 -0
  42. package/src/components/auth/auth-callback.tsx +36 -0
  43. package/src/components/auth/auth-form.tsx +277 -0
  44. package/src/components/auth/auth-view.tsx +389 -0
  45. package/src/components/auth/email-otp-button.tsx +53 -0
  46. package/src/components/auth/forms/email-otp-form.tsx +288 -0
  47. package/src/components/auth/forms/forgot-password-form.tsx +168 -0
  48. package/src/components/auth/forms/magic-link-form.tsx +191 -0
  49. package/src/components/auth/forms/recover-account-form.tsx +138 -0
  50. package/src/components/auth/forms/reset-password-form.tsx +215 -0
  51. package/src/components/auth/forms/sign-in-form.tsx +289 -0
  52. package/src/components/auth/forms/sign-up-form.tsx +788 -0
  53. package/src/components/auth/forms/two-factor-form.tsx +372 -0
  54. package/src/components/auth/magic-link-button.tsx +54 -0
  55. package/src/components/auth/one-tap.tsx +48 -0
  56. package/src/components/auth/otp-input-group.tsx +65 -0
  57. package/src/components/auth/passkey-button.tsx +85 -0
  58. package/src/components/auth/provider-button.tsx +141 -0
  59. package/src/components/auth/sign-out.tsx +25 -0
  60. package/src/components/auth-loading.tsx +21 -0
  61. package/src/components/captcha/captcha.tsx +79 -0
  62. package/src/components/captcha/recaptcha-badge.tsx +61 -0
  63. package/src/components/captcha/recaptcha-v2.tsx +58 -0
  64. package/src/components/captcha/recaptcha-v3.tsx +73 -0
  65. package/src/components/email/email-template.tsx +216 -0
  66. package/src/components/form-error.tsx +27 -0
  67. package/src/components/organization/accept-invitation-card.tsx +362 -0
  68. package/src/components/organization/create-organization-dialog.tsx +395 -0
  69. package/src/components/organization/delete-organization-card.tsx +101 -0
  70. package/src/components/organization/delete-organization-dialog.tsx +209 -0
  71. package/src/components/organization/invitation-cell.tsx +156 -0
  72. package/src/components/organization/invite-member-dialog.tsx +258 -0
  73. package/src/components/organization/leave-organization-dialog.tsx +150 -0
  74. package/src/components/organization/member-cell.tsx +187 -0
  75. package/src/components/organization/organization-cell-view.tsx +122 -0
  76. package/src/components/organization/organization-cell.tsx +154 -0
  77. package/src/components/organization/organization-invitations-card.tsx +94 -0
  78. package/src/components/organization/organization-logo-card.tsx +308 -0
  79. package/src/components/organization/organization-logo.tsx +120 -0
  80. package/src/components/organization/organization-members-card.tsx +155 -0
  81. package/src/components/organization/organization-name-card.tsx +204 -0
  82. package/src/components/organization/organization-settings-cards.tsx +67 -0
  83. package/src/components/organization/organization-slug-card.tsx +223 -0
  84. package/src/components/organization/organization-switcher.tsx +512 -0
  85. package/src/components/organization/organization-view.tsx +228 -0
  86. package/src/components/organization/organizations-card.tsx +72 -0
  87. package/src/components/organization/personal-account-view.tsx +115 -0
  88. package/src/components/organization/remove-member-dialog.tsx +144 -0
  89. package/src/components/organization/update-member-role-dialog.tsx +213 -0
  90. package/src/components/organization/user-invitations-card.tsx +238 -0
  91. package/src/components/password-input.tsx +56 -0
  92. package/src/components/provider-icons.tsx +385 -0
  93. package/src/components/redirect-to-sign-in.tsx +16 -0
  94. package/src/components/redirect-to-sign-up.tsx +16 -0
  95. package/src/components/settings/account/account-cell.tsx +158 -0
  96. package/src/components/settings/account/accounts-card.tsx +75 -0
  97. package/src/components/settings/account/delete-account-card.tsx +65 -0
  98. package/src/components/settings/account/delete-account-dialog.tsx +231 -0
  99. package/src/components/settings/account/update-avatar-card.tsx +198 -0
  100. package/src/components/settings/account/update-field-card.tsx +282 -0
  101. package/src/components/settings/account/update-name-card.tsx +39 -0
  102. package/src/components/settings/account/update-username-card.tsx +42 -0
  103. package/src/components/settings/account-settings-cards.tsx +123 -0
  104. package/src/components/settings/api-key/api-key-cell.tsx +108 -0
  105. package/src/components/settings/api-key/api-key-delete-dialog.tsx +162 -0
  106. package/src/components/settings/api-key/api-key-display-dialog.tsx +110 -0
  107. package/src/components/settings/api-key/api-keys-card.tsx +98 -0
  108. package/src/components/settings/api-key/create-api-key-dialog.tsx +376 -0
  109. package/src/components/settings/passkey/passkey-cell.tsx +113 -0
  110. package/src/components/settings/passkey/passkeys-card.tsx +111 -0
  111. package/src/components/settings/providers/provider-cell.tsx +152 -0
  112. package/src/components/settings/providers/providers-card.tsx +108 -0
  113. package/src/components/settings/security/change-email-card.tsx +200 -0
  114. package/src/components/settings/security/change-password-card.tsx +326 -0
  115. package/src/components/settings/security/session-cell.tsx +120 -0
  116. package/src/components/settings/security/sessions-card.tsx +58 -0
  117. package/src/components/settings/security-settings-cards.tsx +111 -0
  118. package/src/components/settings/shared/session-freshness-dialog.tsx +97 -0
  119. package/src/components/settings/shared/settings-action-button.tsx +51 -0
  120. package/src/components/settings/shared/settings-card-footer.tsx +94 -0
  121. package/src/components/settings/shared/settings-card-header.tsx +67 -0
  122. package/src/components/settings/shared/settings-card.tsx +106 -0
  123. package/src/components/settings/skeletons/input-field-skeleton.tsx +18 -0
  124. package/src/components/settings/skeletons/settings-cell-skeleton.tsx +37 -0
  125. package/src/components/settings/two-factor/backup-codes-dialog.tsx +113 -0
  126. package/src/components/settings/two-factor/two-factor-card.tsx +63 -0
  127. package/src/components/settings/two-factor/two-factor-password-dialog.tsx +226 -0
  128. package/src/components/signed-in.tsx +20 -0
  129. package/src/components/signed-out.tsx +20 -0
  130. package/src/components/ui/alert.tsx +66 -0
  131. package/src/components/ui/avatar.tsx +53 -0
  132. package/src/components/ui/button.tsx +59 -0
  133. package/src/components/ui/card.tsx +92 -0
  134. package/src/components/ui/checkbox.tsx +32 -0
  135. package/src/components/ui/dialog.tsx +143 -0
  136. package/src/components/ui/drawer.tsx +135 -0
  137. package/src/components/ui/dropdown-menu.tsx +257 -0
  138. package/src/components/ui/form.tsx +167 -0
  139. package/src/components/ui/input-otp.tsx +77 -0
  140. package/src/components/ui/input.tsx +21 -0
  141. package/src/components/ui/label.tsx +24 -0
  142. package/src/components/ui/select.tsx +185 -0
  143. package/src/components/ui/separator.tsx +28 -0
  144. package/src/components/ui/skeleton.tsx +13 -0
  145. package/src/components/ui/tabs.tsx +66 -0
  146. package/src/components/ui/textarea.tsx +18 -0
  147. package/src/components/user-avatar.tsx +147 -0
  148. package/src/components/user-button.tsx +409 -0
  149. package/src/components/user-view.tsx +138 -0
  150. package/src/hooks/use-auth-data.ts +184 -0
  151. package/src/hooks/use-authenticate.ts +62 -0
  152. package/src/hooks/use-captcha.tsx +138 -0
  153. package/src/hooks/use-current-organization.ts +59 -0
  154. package/src/hooks/use-hydrated.ts +13 -0
  155. package/src/hooks/use-lang.ts +32 -0
  156. package/src/hooks/use-success-transition.ts +51 -0
  157. package/src/hooks/use-theme.ts +39 -0
  158. package/src/index.ts +65 -0
  159. package/src/instantdb.ts +1 -0
  160. package/src/lib/auth-data-cache.ts +90 -0
  161. package/src/lib/auth-ui-provider.tsx +658 -0
  162. package/src/lib/gravatar-utils.ts +58 -0
  163. package/src/lib/image-utils.ts +55 -0
  164. package/src/lib/instantdb/model-names.ts +24 -0
  165. package/src/lib/instantdb/use-instant-options.ts +98 -0
  166. package/src/lib/instantdb/use-list-accounts.ts +38 -0
  167. package/src/lib/instantdb/use-list-sessions.ts +53 -0
  168. package/src/lib/instantdb/use-session.ts +55 -0
  169. package/src/lib/organization-refetcher.tsx +56 -0
  170. package/src/lib/social-providers.ts +144 -0
  171. package/src/lib/tanstack/auth-ui-provider-tanstack.tsx +49 -0
  172. package/src/lib/tanstack/use-tanstack-options.ts +112 -0
  173. package/src/lib/triplit/model-names.ts +24 -0
  174. package/src/lib/triplit/use-conditional-query.ts +82 -0
  175. package/src/lib/triplit/use-list-accounts.ts +31 -0
  176. package/src/lib/triplit/use-list-sessions.ts +33 -0
  177. package/src/lib/triplit/use-session.ts +42 -0
  178. package/src/lib/triplit/use-triplit-hooks.ts +68 -0
  179. package/src/lib/triplit/use-triplit-token.ts +44 -0
  180. package/src/lib/utils.ts +105 -0
  181. package/src/lib/view-paths.ts +55 -0
  182. package/src/localization/admin-error-codes.ts +20 -0
  183. package/src/localization/anonymous-error-codes.ts +6 -0
  184. package/src/localization/api-key-error-codes.ts +32 -0
  185. package/src/localization/auth-localization.ts +740 -0
  186. package/src/localization/base-error-codes.ts +27 -0
  187. package/src/localization/captcha-error-codes.ts +17 -0
  188. package/src/localization/email-otp-error-codes.ts +7 -0
  189. package/src/localization/generic-oauth-error-codes.ts +3 -0
  190. package/src/localization/haveibeenpwned-error-codes.ts +4 -0
  191. package/src/localization/multi-session-error-codes.ts +3 -0
  192. package/src/localization/organization-error-codes.ts +57 -0
  193. package/src/localization/passkey-error-codes.ts +10 -0
  194. package/src/localization/phone-number-error-codes.ts +10 -0
  195. package/src/localization/stripe-localization.ts +12 -0
  196. package/src/localization/two-factor-error-codes.ts +12 -0
  197. package/src/localization/username-error-codes.ts +9 -0
  198. package/src/server.ts +4 -0
  199. package/src/style.css +1 -0
  200. package/src/tanstack.ts +1 -0
  201. package/src/triplit.ts +1 -0
  202. package/src/types/account-options.ts +35 -0
  203. package/src/types/additional-fields.ts +21 -0
  204. package/src/types/any-auth-client.ts +6 -0
  205. package/src/types/api-key.ts +9 -0
  206. package/src/types/auth-client.ts +37 -0
  207. package/src/types/auth-hooks.ts +61 -0
  208. package/src/types/auth-mutators.ts +17 -0
  209. package/src/types/avatar-options.ts +29 -0
  210. package/src/types/captcha-options.ts +32 -0
  211. package/src/types/captcha-provider.ts +6 -0
  212. package/src/types/credentials-options.ts +32 -0
  213. package/src/types/delete-user-options.ts +7 -0
  214. package/src/types/fetch-error.ts +6 -0
  215. package/src/types/generic-oauth-options.ts +16 -0
  216. package/src/types/gravatar-options.ts +21 -0
  217. package/src/types/image.ts +7 -0
  218. package/src/types/invitation.ts +10 -0
  219. package/src/types/link.ts +7 -0
  220. package/src/types/organization-options.ts +106 -0
  221. package/src/types/password-validation.ts +16 -0
  222. package/src/types/profile.ts +15 -0
  223. package/src/types/refetch.ts +1 -0
  224. package/src/types/render-toast.ts +9 -0
  225. package/src/types/sign-up-options.ts +7 -0
  226. package/src/types/social-options.ts +16 -0
@@ -0,0 +1,376 @@
1
+ "use client"
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod"
4
+ import type { Organization } from "better-auth/plugins/organization"
5
+ import { Loader2 } from "lucide-react"
6
+ import { type ComponentProps, useContext } from "react"
7
+ import { useForm } from "react-hook-form"
8
+ import * as z from "zod"
9
+ import { useLang } from "../../../hooks/use-lang"
10
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
11
+ import { cn, getLocalizedError } from "../../../lib/utils"
12
+ import type { AuthLocalization } from "../../../localization/auth-localization"
13
+ import type { Refetch } from "../../../types/refetch"
14
+ import { OrganizationCellView } from "../../organization/organization-cell-view"
15
+ import { PersonalAccountView } from "../../organization/personal-account-view"
16
+ import { Button } from "../../ui/button"
17
+ import {
18
+ Dialog,
19
+ DialogContent,
20
+ DialogDescription,
21
+ DialogFooter,
22
+ DialogHeader,
23
+ DialogTitle
24
+ } from "../../ui/dialog"
25
+ import {
26
+ Form,
27
+ FormControl,
28
+ FormField,
29
+ FormItem,
30
+ FormLabel,
31
+ FormMessage
32
+ } from "../../ui/form"
33
+ import { Input } from "../../ui/input"
34
+ import {
35
+ Select,
36
+ SelectContent,
37
+ SelectItem,
38
+ SelectTrigger,
39
+ SelectValue
40
+ } from "../../ui/select"
41
+ import type { SettingsCardClassNames } from "../shared/settings-card"
42
+
43
+ interface CreateApiKeyDialogProps extends ComponentProps<typeof Dialog> {
44
+ classNames?: SettingsCardClassNames
45
+ localization?: AuthLocalization
46
+ onSuccess: (key: string) => void
47
+ refetch?: Refetch
48
+ organizationId?: string
49
+ }
50
+
51
+ export function CreateApiKeyDialog({
52
+ classNames,
53
+ localization,
54
+ onSuccess,
55
+ refetch,
56
+ organizationId,
57
+ onOpenChange,
58
+ ...props
59
+ }: CreateApiKeyDialogProps) {
60
+ const {
61
+ authClient,
62
+ apiKey,
63
+ hooks: { useListOrganizations, useSession },
64
+ localization: contextLocalization,
65
+ organization: contextOrganization,
66
+ toast
67
+ } = useContext(AuthUIContext)
68
+
69
+ localization = { ...contextLocalization, ...localization }
70
+
71
+ const { lang } = useLang()
72
+
73
+ let organizations: Organization[] | null | undefined
74
+ if (contextOrganization) {
75
+ const { data } = useListOrganizations()
76
+ organizations = data
77
+ }
78
+
79
+ const { data: sessionData } = useSession()
80
+ const user = sessionData?.user
81
+
82
+ const showOrganizationSelect = contextOrganization?.apiKey
83
+
84
+ const formSchema = z.object({
85
+ name: z
86
+ .string()
87
+ .min(1, `${localization.NAME} ${localization.IS_REQUIRED}`),
88
+ expiresInDays: z.string().optional(),
89
+ organizationId: showOrganizationSelect
90
+ ? z
91
+ .string()
92
+ .min(
93
+ 1,
94
+ `${localization.ORGANIZATION} ${localization.IS_REQUIRED}`
95
+ )
96
+ : z.string().optional()
97
+ })
98
+
99
+ const form = useForm({
100
+ resolver: zodResolver(formSchema),
101
+ values: {
102
+ name: "",
103
+ expiresInDays: "none",
104
+ organizationId: organizationId ?? "personal"
105
+ }
106
+ })
107
+
108
+ const { isSubmitting } = form.formState
109
+
110
+ const onSubmit = async (values: z.infer<typeof formSchema>) => {
111
+ try {
112
+ const expiresIn =
113
+ values.expiresInDays && values.expiresInDays !== "none"
114
+ ? Number.parseInt(values.expiresInDays) * 60 * 60 * 24
115
+ : undefined
116
+
117
+ const selectedOrgId =
118
+ values.organizationId === "personal"
119
+ ? undefined
120
+ : values.organizationId
121
+
122
+ const metadata = {
123
+ ...(typeof apiKey === "object" ? apiKey.metadata : {}),
124
+ ...(contextOrganization && selectedOrgId
125
+ ? { organizationId: selectedOrgId }
126
+ : {})
127
+ }
128
+
129
+ const result = await authClient.apiKey.create({
130
+ name: values.name,
131
+ expiresIn,
132
+ prefix: typeof apiKey === "object" ? apiKey.prefix : undefined,
133
+ metadata:
134
+ Object.keys(metadata).length > 0 ? metadata : undefined,
135
+ fetchOptions: { throw: true }
136
+ })
137
+
138
+ await refetch?.()
139
+ onSuccess(result.key)
140
+ onOpenChange?.(false)
141
+ form.reset()
142
+ } catch (error) {
143
+ toast({
144
+ variant: "error",
145
+ message: getLocalizedError({ error, localization })
146
+ })
147
+ }
148
+ }
149
+
150
+ const rtf = new Intl.RelativeTimeFormat(lang ?? "en")
151
+
152
+ return (
153
+ <Dialog onOpenChange={onOpenChange} {...props}>
154
+ <DialogContent
155
+ onOpenAutoFocus={(e) => e.preventDefault()}
156
+ className={classNames?.dialog?.content}
157
+ >
158
+ <DialogHeader className={classNames?.dialog?.header}>
159
+ <DialogTitle
160
+ className={cn("text-lg md:text-xl", classNames?.title)}
161
+ >
162
+ {localization.CREATE_API_KEY}
163
+ </DialogTitle>
164
+
165
+ <DialogDescription
166
+ className={cn(
167
+ "text-xs md:text-sm",
168
+ classNames?.description
169
+ )}
170
+ >
171
+ {localization.CREATE_API_KEY_DESCRIPTION}
172
+ </DialogDescription>
173
+ </DialogHeader>
174
+
175
+ <Form {...form}>
176
+ <form
177
+ onSubmit={form.handleSubmit(onSubmit)}
178
+ className="space-y-6"
179
+ >
180
+ {showOrganizationSelect && (
181
+ <FormField
182
+ control={form.control}
183
+ name="organizationId"
184
+ render={({ field }) => (
185
+ <FormItem className="w-full">
186
+ <FormLabel
187
+ className={classNames?.label}
188
+ >
189
+ {localization.ORGANIZATION}
190
+ </FormLabel>
191
+
192
+ <Select
193
+ onValueChange={field.onChange}
194
+ value={field.value}
195
+ disabled={isSubmitting}
196
+ >
197
+ <FormControl>
198
+ <SelectTrigger
199
+ className={cn(
200
+ "w-full p-2",
201
+ classNames?.input
202
+ )}
203
+ >
204
+ <SelectValue
205
+ placeholder={
206
+ localization.ORGANIZATION
207
+ }
208
+ />
209
+ </SelectTrigger>
210
+ </FormControl>
211
+
212
+ <SelectContent className="w-[--radix-select-trigger-width]">
213
+ <SelectItem
214
+ value="personal"
215
+ className="p-2"
216
+ >
217
+ <PersonalAccountView
218
+ user={user}
219
+ localization={
220
+ localization
221
+ }
222
+ size="sm"
223
+ />
224
+ </SelectItem>
225
+
226
+ {organizations?.map((org) => (
227
+ <SelectItem
228
+ key={org.id}
229
+ value={org.id}
230
+ className="p-2"
231
+ >
232
+ <OrganizationCellView
233
+ organization={org}
234
+ localization={
235
+ localization
236
+ }
237
+ size="sm"
238
+ />
239
+ </SelectItem>
240
+ ))}
241
+ </SelectContent>
242
+ </Select>
243
+
244
+ <FormMessage />
245
+ </FormItem>
246
+ )}
247
+ />
248
+ )}
249
+
250
+ <div className="flex gap-4">
251
+ <FormField
252
+ control={form.control}
253
+ name="name"
254
+ render={({ field }) => (
255
+ <FormItem className="flex-1">
256
+ <FormLabel
257
+ className={classNames?.label}
258
+ >
259
+ {localization.NAME}
260
+ </FormLabel>
261
+
262
+ <FormControl>
263
+ <Input
264
+ className={classNames?.input}
265
+ placeholder={
266
+ localization.API_KEY_NAME_PLACEHOLDER
267
+ }
268
+ autoFocus
269
+ disabled={isSubmitting}
270
+ {...field}
271
+ />
272
+ </FormControl>
273
+
274
+ <FormMessage />
275
+ </FormItem>
276
+ )}
277
+ />
278
+
279
+ <FormField
280
+ control={form.control}
281
+ name="expiresInDays"
282
+ render={({ field }) => (
283
+ <FormItem>
284
+ <FormLabel
285
+ className={classNames?.label}
286
+ >
287
+ {localization.EXPIRES}
288
+ </FormLabel>
289
+
290
+ <Select
291
+ onValueChange={field.onChange}
292
+ defaultValue={field.value}
293
+ disabled={isSubmitting}
294
+ >
295
+ <FormControl>
296
+ <SelectTrigger
297
+ className={
298
+ classNames?.input
299
+ }
300
+ >
301
+ <SelectValue
302
+ placeholder={
303
+ localization.NO_EXPIRATION
304
+ }
305
+ />
306
+ </SelectTrigger>
307
+ </FormControl>
308
+
309
+ <SelectContent>
310
+ <SelectItem value="none">
311
+ {localization.NO_EXPIRATION}
312
+ </SelectItem>
313
+
314
+ {[
315
+ 1, 7, 30, 60, 90, 180, 365
316
+ ].map((days) => (
317
+ <SelectItem
318
+ key={days}
319
+ value={days.toString()}
320
+ >
321
+ {days === 365
322
+ ? rtf.format(
323
+ 1,
324
+ "year"
325
+ )
326
+ : rtf.format(
327
+ days,
328
+ "day"
329
+ )}
330
+ </SelectItem>
331
+ ))}
332
+ </SelectContent>
333
+ </Select>
334
+
335
+ <FormMessage />
336
+ </FormItem>
337
+ )}
338
+ />
339
+ </div>
340
+
341
+ <DialogFooter className={classNames?.dialog?.footer}>
342
+ <Button
343
+ type="button"
344
+ variant="outline"
345
+ onClick={() => onOpenChange?.(false)}
346
+ className={cn(
347
+ classNames?.button,
348
+ classNames?.outlineButton
349
+ )}
350
+ disabled={isSubmitting}
351
+ >
352
+ {localization.CANCEL}
353
+ </Button>
354
+
355
+ <Button
356
+ type="submit"
357
+ variant="default"
358
+ className={cn(
359
+ classNames?.button,
360
+ classNames?.primaryButton
361
+ )}
362
+ disabled={isSubmitting}
363
+ >
364
+ {isSubmitting && (
365
+ <Loader2 className="animate-spin" />
366
+ )}
367
+
368
+ {localization.CREATE_API_KEY}
369
+ </Button>
370
+ </DialogFooter>
371
+ </form>
372
+ </Form>
373
+ </DialogContent>
374
+ </Dialog>
375
+ )
376
+ }
@@ -0,0 +1,113 @@
1
+ "use client"
2
+
3
+ import { FingerprintIcon, Loader2 } from "lucide-react"
4
+ import { useContext, useState } from "react"
5
+
6
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
7
+ import { cn, getLocalizedError } from "../../../lib/utils"
8
+ import type { AuthLocalization } from "../../../localization/auth-localization"
9
+ import { Button } from "../../ui/button"
10
+ import { Card } from "../../ui/card"
11
+ import { SessionFreshnessDialog } from "../shared/session-freshness-dialog"
12
+ import type { SettingsCardClassNames } from "../shared/settings-card"
13
+
14
+ export interface PasskeyCellProps {
15
+ className?: string
16
+ classNames?: SettingsCardClassNames
17
+ localization?: Partial<AuthLocalization>
18
+ passkey: { id: string; createdAt: Date }
19
+ }
20
+
21
+ export function PasskeyCell({
22
+ className,
23
+ classNames,
24
+ localization,
25
+ passkey
26
+ }: PasskeyCellProps) {
27
+ const {
28
+ freshAge,
29
+ hooks: { useSession, useListPasskeys },
30
+ localization: contextLocalization,
31
+ mutators: { deletePasskey },
32
+ toast
33
+ } = useContext(AuthUIContext)
34
+
35
+ localization = { ...contextLocalization, ...localization }
36
+
37
+ const { refetch } = useListPasskeys()
38
+
39
+ const { data: sessionData } = useSession()
40
+ const session = sessionData?.session
41
+ const isFresh = session
42
+ ? Date.now() - new Date(session?.createdAt).getTime() < freshAge * 1000
43
+ : false
44
+
45
+ const [showFreshnessDialog, setShowFreshnessDialog] = useState(false)
46
+ const [isLoading, setIsLoading] = useState(false)
47
+
48
+ const handleDeletePasskey = async () => {
49
+ // If session isn't fresh, show the freshness dialog
50
+ if (!isFresh) {
51
+ setShowFreshnessDialog(true)
52
+ return
53
+ }
54
+
55
+ setIsLoading(true)
56
+
57
+ try {
58
+ await deletePasskey({ id: passkey.id })
59
+ refetch?.()
60
+ } catch (error) {
61
+ setIsLoading(false)
62
+
63
+ toast({
64
+ variant: "error",
65
+ message: getLocalizedError({ error, localization })
66
+ })
67
+ }
68
+ }
69
+
70
+ return (
71
+ <>
72
+ <SessionFreshnessDialog
73
+ open={showFreshnessDialog}
74
+ onOpenChange={setShowFreshnessDialog}
75
+ classNames={classNames}
76
+ localization={localization}
77
+ />
78
+
79
+ <Card
80
+ className={cn(
81
+ "flex-row items-center p-4",
82
+ className,
83
+ classNames?.cell
84
+ )}
85
+ >
86
+ <div className="flex items-center gap-3">
87
+ <FingerprintIcon
88
+ className={cn("size-4", classNames?.icon)}
89
+ />
90
+ <span className="text-sm">
91
+ {new Date(passkey.createdAt).toLocaleString()}
92
+ </span>
93
+ </div>
94
+
95
+ <Button
96
+ className={cn(
97
+ "relative ms-auto",
98
+ classNames?.button,
99
+ classNames?.outlineButton
100
+ )}
101
+ disabled={isLoading}
102
+ size="sm"
103
+ variant="outline"
104
+ onClick={handleDeletePasskey}
105
+ >
106
+ {isLoading && <Loader2 className="animate-spin" />}
107
+
108
+ {localization.DELETE}
109
+ </Button>
110
+ </Card>
111
+ </>
112
+ )
113
+ }
@@ -0,0 +1,111 @@
1
+ "use client"
2
+
3
+ import { useContext, useState } from "react"
4
+ import { useForm } from "react-hook-form"
5
+
6
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
7
+ import { cn, getLocalizedError } from "../../../lib/utils"
8
+ import type { AuthLocalization } from "../../../localization/auth-localization"
9
+ import { CardContent } from "../../ui/card"
10
+ import { Form } from "../../ui/form"
11
+ import { SessionFreshnessDialog } from "../shared/session-freshness-dialog"
12
+ import type { SettingsCardClassNames } from "../shared/settings-card"
13
+ import { SettingsCard } from "../shared/settings-card"
14
+ import { PasskeyCell } from "./passkey-cell"
15
+
16
+ export interface PasskeysCardProps {
17
+ className?: string
18
+ classNames?: SettingsCardClassNames
19
+ localization?: AuthLocalization
20
+ }
21
+
22
+ export function PasskeysCard({
23
+ className,
24
+ classNames,
25
+ localization
26
+ }: PasskeysCardProps) {
27
+ const {
28
+ authClient,
29
+ freshAge,
30
+ hooks: { useListPasskeys, useSession },
31
+ localization: authLocalization,
32
+ toast
33
+ } = useContext(AuthUIContext)
34
+
35
+ localization = { ...authLocalization, ...localization }
36
+
37
+ const { data: passkeys, isPending, refetch } = useListPasskeys()
38
+
39
+ const { data: sessionData } = useSession()
40
+ const session = sessionData?.session
41
+ const isFresh = session
42
+ ? Date.now() - new Date(session?.createdAt).getTime() < freshAge * 1000
43
+ : false
44
+
45
+ const [showFreshnessDialog, setShowFreshnessDialog] = useState(false)
46
+
47
+ const addPasskey = async () => {
48
+ // If session isn't fresh, show the freshness dialog
49
+ if (!isFresh) {
50
+ setShowFreshnessDialog(true)
51
+ return
52
+ }
53
+
54
+ try {
55
+ await authClient.passkey.addPasskey({
56
+ fetchOptions: { throw: true }
57
+ })
58
+ await refetch?.()
59
+ } catch (error) {
60
+ toast({
61
+ variant: "error",
62
+ message: getLocalizedError({ error, localization })
63
+ })
64
+ }
65
+ }
66
+
67
+ const form = useForm()
68
+
69
+ return (
70
+ <>
71
+ <SessionFreshnessDialog
72
+ open={showFreshnessDialog}
73
+ onOpenChange={setShowFreshnessDialog}
74
+ classNames={classNames}
75
+ localization={localization}
76
+ />
77
+
78
+ <Form {...form}>
79
+ <form onSubmit={form.handleSubmit(addPasskey)}>
80
+ <SettingsCard
81
+ className={className}
82
+ classNames={classNames}
83
+ actionLabel={localization.ADD_PASSKEY}
84
+ description={localization.PASSKEYS_DESCRIPTION}
85
+ instructions={localization.PASSKEYS_INSTRUCTIONS}
86
+ isPending={isPending}
87
+ title={localization.PASSKEYS}
88
+ >
89
+ {passkeys && passkeys.length > 0 && (
90
+ <CardContent
91
+ className={cn(
92
+ "grid gap-4",
93
+ classNames?.content
94
+ )}
95
+ >
96
+ {passkeys?.map((passkey) => (
97
+ <PasskeyCell
98
+ key={passkey.id}
99
+ classNames={classNames}
100
+ localization={localization}
101
+ passkey={passkey}
102
+ />
103
+ ))}
104
+ </CardContent>
105
+ )}
106
+ </SettingsCard>
107
+ </form>
108
+ </Form>
109
+ </>
110
+ )
111
+ }