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,326 @@
1
+ "use client"
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod"
4
+ import { useContext } from "react"
5
+ import { useForm } from "react-hook-form"
6
+ import * as z from "zod"
7
+
8
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
9
+ import { cn, getLocalizedError, getPasswordSchema } from "../../../lib/utils"
10
+ import type { AuthLocalization } from "../../../localization/auth-localization"
11
+ import type { PasswordValidation } from "../../../types/password-validation"
12
+ import { PasswordInput } from "../../password-input"
13
+ import { CardContent } from "../../ui/card"
14
+ import {
15
+ Form,
16
+ FormControl,
17
+ FormField,
18
+ FormItem,
19
+ FormLabel,
20
+ FormMessage
21
+ } from "../../ui/form"
22
+ import {
23
+ SettingsCard,
24
+ type SettingsCardClassNames
25
+ } from "../shared/settings-card"
26
+ import { InputFieldSkeleton } from "../skeletons/input-field-skeleton"
27
+
28
+ export interface ChangePasswordCardProps {
29
+ className?: string
30
+ classNames?: SettingsCardClassNames
31
+ accounts?: { providerId: string }[] | null
32
+ isPending?: boolean
33
+ localization?: AuthLocalization
34
+ skipHook?: boolean
35
+ passwordValidation?: PasswordValidation
36
+ }
37
+
38
+ export function ChangePasswordCard({
39
+ className,
40
+ classNames,
41
+ accounts,
42
+ isPending,
43
+ localization,
44
+ skipHook,
45
+ passwordValidation
46
+ }: ChangePasswordCardProps) {
47
+ const {
48
+ authClient,
49
+ basePath,
50
+ baseURL,
51
+ credentials,
52
+ hooks: { useSession, useListAccounts },
53
+ localization: contextLocalization,
54
+ viewPaths,
55
+ toast
56
+ } = useContext(AuthUIContext)
57
+
58
+ const confirmPasswordEnabled = credentials?.confirmPassword
59
+ const contextPasswordValidation = credentials?.passwordValidation
60
+
61
+ localization = { ...contextLocalization, ...localization }
62
+ passwordValidation = { ...contextPasswordValidation, ...passwordValidation }
63
+
64
+ const { data: sessionData } = useSession()
65
+
66
+ if (!skipHook) {
67
+ const result = useListAccounts()
68
+ accounts = result.data
69
+ isPending = result.isPending
70
+ }
71
+
72
+ const formSchema = z
73
+ .object({
74
+ currentPassword: getPasswordSchema(
75
+ passwordValidation,
76
+ localization
77
+ ),
78
+ newPassword: getPasswordSchema(passwordValidation, {
79
+ PASSWORD_REQUIRED: localization.NEW_PASSWORD_REQUIRED,
80
+ PASSWORD_TOO_SHORT: localization.PASSWORD_TOO_SHORT,
81
+ PASSWORD_TOO_LONG: localization.PASSWORD_TOO_LONG,
82
+ INVALID_PASSWORD: localization.INVALID_PASSWORD
83
+ }),
84
+ confirmPassword: confirmPasswordEnabled
85
+ ? getPasswordSchema(passwordValidation, {
86
+ PASSWORD_REQUIRED: localization.CONFIRM_PASSWORD_REQUIRED,
87
+ PASSWORD_TOO_SHORT: localization.PASSWORD_TOO_SHORT,
88
+ PASSWORD_TOO_LONG: localization.PASSWORD_TOO_LONG,
89
+ INVALID_PASSWORD: localization.INVALID_PASSWORD
90
+ })
91
+ : z.string().optional()
92
+ })
93
+ .refine(
94
+ (data) =>
95
+ !confirmPasswordEnabled ||
96
+ data.newPassword === data.confirmPassword,
97
+ {
98
+ message: localization.PASSWORDS_DO_NOT_MATCH,
99
+ path: ["confirmPassword"]
100
+ }
101
+ )
102
+
103
+ const form = useForm({
104
+ resolver: zodResolver(formSchema),
105
+ defaultValues: {
106
+ currentPassword: "",
107
+ newPassword: "",
108
+ confirmPassword: ""
109
+ }
110
+ })
111
+
112
+ const setPasswordForm = useForm()
113
+
114
+ const { isSubmitting } = form.formState
115
+
116
+ const setPassword = async () => {
117
+ if (!sessionData) return
118
+ const email = sessionData?.user.email
119
+
120
+ try {
121
+ await authClient.requestPasswordReset({
122
+ email,
123
+ redirectTo: `${baseURL}${basePath}/${viewPaths.RESET_PASSWORD}`,
124
+ fetchOptions: { throw: true }
125
+ })
126
+
127
+ toast({
128
+ variant: "success",
129
+ message: localization.FORGOT_PASSWORD_EMAIL!
130
+ })
131
+ } catch (error) {
132
+ toast({
133
+ variant: "error",
134
+ message: getLocalizedError({ error, localization })
135
+ })
136
+ }
137
+ }
138
+
139
+ const changePassword = async ({
140
+ currentPassword,
141
+ newPassword
142
+ }: z.infer<typeof formSchema>) => {
143
+ try {
144
+ await authClient.changePassword({
145
+ currentPassword,
146
+ newPassword,
147
+ revokeOtherSessions: true,
148
+ fetchOptions: { throw: true }
149
+ })
150
+
151
+ toast({
152
+ variant: "success",
153
+ message: localization.CHANGE_PASSWORD_SUCCESS!
154
+ })
155
+ } catch (error) {
156
+ toast({
157
+ variant: "error",
158
+ message: getLocalizedError({ error, localization })
159
+ })
160
+ }
161
+
162
+ form.reset()
163
+ }
164
+
165
+ const credentialsLinked = accounts?.some(
166
+ (acc) => acc.providerId === "credential"
167
+ )
168
+
169
+ if (!isPending && !credentialsLinked) {
170
+ return (
171
+ <Form {...setPasswordForm}>
172
+ <form onSubmit={setPasswordForm.handleSubmit(setPassword)}>
173
+ <SettingsCard
174
+ title={localization.SET_PASSWORD}
175
+ description={localization.SET_PASSWORD_DESCRIPTION}
176
+ actionLabel={localization.SET_PASSWORD}
177
+ isPending={isPending}
178
+ className={className}
179
+ classNames={classNames}
180
+ />
181
+ </form>
182
+ </Form>
183
+ )
184
+ }
185
+
186
+ return (
187
+ <Form {...form}>
188
+ <form onSubmit={form.handleSubmit(changePassword)}>
189
+ <SettingsCard
190
+ className={className}
191
+ classNames={classNames}
192
+ actionLabel={localization.SAVE}
193
+ description={localization.CHANGE_PASSWORD_DESCRIPTION}
194
+ instructions={localization.CHANGE_PASSWORD_INSTRUCTIONS}
195
+ isPending={isPending}
196
+ title={localization.CHANGE_PASSWORD}
197
+ >
198
+ <CardContent
199
+ className={cn("grid gap-6", classNames?.content)}
200
+ >
201
+ {isPending || !accounts ? (
202
+ <>
203
+ <InputFieldSkeleton classNames={classNames} />
204
+ <InputFieldSkeleton classNames={classNames} />
205
+
206
+ {confirmPasswordEnabled && (
207
+ <InputFieldSkeleton
208
+ classNames={classNames}
209
+ />
210
+ )}
211
+ </>
212
+ ) : (
213
+ <>
214
+ <FormField
215
+ control={form.control}
216
+ name="currentPassword"
217
+ render={({ field }) => (
218
+ <FormItem>
219
+ <FormLabel
220
+ className={classNames?.label}
221
+ >
222
+ {localization.CURRENT_PASSWORD}
223
+ </FormLabel>
224
+
225
+ <FormControl>
226
+ <PasswordInput
227
+ className={
228
+ classNames?.input
229
+ }
230
+ autoComplete="current-password"
231
+ placeholder={
232
+ localization.CURRENT_PASSWORD_PLACEHOLDER
233
+ }
234
+ disabled={isSubmitting}
235
+ {...field}
236
+ />
237
+ </FormControl>
238
+
239
+ <FormMessage
240
+ className={classNames?.error}
241
+ />
242
+ </FormItem>
243
+ )}
244
+ />
245
+
246
+ <FormField
247
+ control={form.control}
248
+ name="newPassword"
249
+ render={({ field }) => (
250
+ <FormItem>
251
+ <FormLabel
252
+ className={classNames?.label}
253
+ >
254
+ {localization.NEW_PASSWORD}
255
+ </FormLabel>
256
+
257
+ <FormControl>
258
+ <PasswordInput
259
+ className={
260
+ classNames?.input
261
+ }
262
+ autoComplete="new-password"
263
+ disabled={isSubmitting}
264
+ placeholder={
265
+ localization.NEW_PASSWORD_PLACEHOLDER
266
+ }
267
+ enableToggle
268
+ {...field}
269
+ />
270
+ </FormControl>
271
+
272
+ <FormMessage
273
+ className={classNames?.error}
274
+ />
275
+ </FormItem>
276
+ )}
277
+ />
278
+
279
+ {confirmPasswordEnabled && (
280
+ <FormField
281
+ control={form.control}
282
+ name="confirmPassword"
283
+ render={({ field }) => (
284
+ <FormItem>
285
+ <FormLabel
286
+ className={
287
+ classNames?.label
288
+ }
289
+ >
290
+ {
291
+ localization.CONFIRM_PASSWORD
292
+ }
293
+ </FormLabel>
294
+
295
+ <FormControl>
296
+ <PasswordInput
297
+ className={
298
+ classNames?.input
299
+ }
300
+ autoComplete="new-password"
301
+ placeholder={
302
+ localization.CONFIRM_PASSWORD_PLACEHOLDER
303
+ }
304
+ disabled={isSubmitting}
305
+ enableToggle
306
+ {...field}
307
+ />
308
+ </FormControl>
309
+
310
+ <FormMessage
311
+ className={
312
+ classNames?.error
313
+ }
314
+ />
315
+ </FormItem>
316
+ )}
317
+ />
318
+ )}
319
+ </>
320
+ )}
321
+ </CardContent>
322
+ </SettingsCard>
323
+ </form>
324
+ </Form>
325
+ )
326
+ }
@@ -0,0 +1,120 @@
1
+ "use client"
2
+
3
+ import type { Session } from "better-auth"
4
+ import { LaptopIcon, Loader2, SmartphoneIcon } from "lucide-react"
5
+ import { useContext, useState } from "react"
6
+ import { UAParser } from "ua-parser-js"
7
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
8
+ import { cn, getLocalizedError } from "../../../lib/utils"
9
+ import type { AuthLocalization } from "../../../localization/auth-localization"
10
+ import type { Refetch } from "../../../types/refetch"
11
+ import { Button } from "../../ui/button"
12
+ import { Card } from "../../ui/card"
13
+ import type { SettingsCardClassNames } from "../shared/settings-card"
14
+
15
+ export interface SessionCellProps {
16
+ className?: string
17
+ classNames?: SettingsCardClassNames
18
+ localization?: Partial<AuthLocalization>
19
+ session: Session
20
+ refetch?: Refetch
21
+ }
22
+
23
+ export function SessionCell({
24
+ className,
25
+ classNames,
26
+ localization,
27
+ session,
28
+ refetch
29
+ }: SessionCellProps) {
30
+ const {
31
+ basePath,
32
+ hooks: { useSession },
33
+ localization: contextLocalization,
34
+ mutators: { revokeSession },
35
+ viewPaths,
36
+ navigate,
37
+ toast
38
+ } = useContext(AuthUIContext)
39
+
40
+ localization = { ...contextLocalization, ...localization }
41
+
42
+ const { data: sessionData } = useSession()
43
+ const isCurrentSession = session.id === sessionData?.session?.id
44
+
45
+ const [isLoading, setIsLoading] = useState(false)
46
+
47
+ const handleRevoke = async () => {
48
+ setIsLoading(true)
49
+
50
+ if (isCurrentSession) {
51
+ navigate(`${basePath}/${viewPaths.SIGN_OUT}`)
52
+ return
53
+ }
54
+
55
+ try {
56
+ await revokeSession({ token: session.token })
57
+ refetch?.()
58
+ } catch (error) {
59
+ toast({
60
+ variant: "error",
61
+ message: getLocalizedError({ error, localization })
62
+ })
63
+
64
+ setIsLoading(false)
65
+ }
66
+ }
67
+
68
+ const parser = UAParser(session.userAgent as string)
69
+ const isMobile = parser.device.type === "mobile"
70
+
71
+ return (
72
+ <Card
73
+ className={cn(
74
+ "flex-row items-center gap-3 px-4 py-3",
75
+ className,
76
+ classNames?.cell
77
+ )}
78
+ >
79
+ {isMobile ? (
80
+ <SmartphoneIcon className={cn("size-4", classNames?.icon)} />
81
+ ) : (
82
+ <LaptopIcon className={cn("size-4", classNames?.icon)} />
83
+ )}
84
+
85
+ <div className="flex flex-col">
86
+ <span className="font-semibold text-sm">
87
+ {isCurrentSession
88
+ ? localization.CURRENT_SESSION
89
+ : session?.ipAddress}
90
+ </span>
91
+
92
+ <span className="text-muted-foreground text-xs">
93
+ {session.userAgent?.includes("tauri-plugin-http")
94
+ ? localization.APP
95
+ : parser.os.name && parser.browser.name
96
+ ? `${parser.os.name}, ${parser.browser.name}`
97
+ : parser.os.name ||
98
+ parser.browser.name ||
99
+ session.userAgent ||
100
+ localization.UNKNOWN}
101
+ </span>
102
+ </div>
103
+
104
+ <Button
105
+ className={cn(
106
+ "relative ms-auto",
107
+ classNames?.button,
108
+ classNames?.outlineButton
109
+ )}
110
+ disabled={isLoading}
111
+ size="sm"
112
+ variant="outline"
113
+ onClick={handleRevoke}
114
+ >
115
+ {isLoading && <Loader2 className="animate-spin" />}
116
+ {isCurrentSession ? localization.SIGN_OUT : localization.REVOKE}
117
+ </Button>
118
+ </Card>
119
+ )
120
+ }
@@ -0,0 +1,58 @@
1
+ "use client"
2
+ import { useContext } from "react"
3
+
4
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
5
+ import { cn } from "../../../lib/utils"
6
+ import type { AuthLocalization } from "../../../localization/auth-localization"
7
+ import { CardContent } from "../../ui/card"
8
+ import type { SettingsCardClassNames } from "../shared/settings-card"
9
+ import { SettingsCard } from "../shared/settings-card"
10
+ import { SettingsCellSkeleton } from "../skeletons/settings-cell-skeleton"
11
+ import { SessionCell } from "./session-cell"
12
+
13
+ export interface SessionsCardProps {
14
+ className?: string
15
+ classNames?: SettingsCardClassNames
16
+ localization?: Partial<AuthLocalization>
17
+ }
18
+
19
+ export function SessionsCard({
20
+ className,
21
+ classNames,
22
+ localization
23
+ }: SessionsCardProps) {
24
+ const {
25
+ hooks: { useListSessions },
26
+ localization: contextLocalization
27
+ } = useContext(AuthUIContext)
28
+
29
+ localization = { ...contextLocalization, ...localization }
30
+
31
+ const { data: sessions, isPending, refetch } = useListSessions()
32
+
33
+ return (
34
+ <SettingsCard
35
+ className={className}
36
+ classNames={classNames}
37
+ description={localization.SESSIONS_DESCRIPTION}
38
+ isPending={isPending}
39
+ title={localization.SESSIONS}
40
+ >
41
+ <CardContent className={cn("grid gap-4", classNames?.content)}>
42
+ {isPending ? (
43
+ <SettingsCellSkeleton classNames={classNames} />
44
+ ) : (
45
+ sessions?.map((session) => (
46
+ <SessionCell
47
+ key={session.id}
48
+ classNames={classNames}
49
+ localization={localization}
50
+ session={session}
51
+ refetch={refetch}
52
+ />
53
+ ))
54
+ )}
55
+ </CardContent>
56
+ </SettingsCard>
57
+ )
58
+ }
@@ -0,0 +1,111 @@
1
+ "use client"
2
+
3
+ import { useContext } from "react"
4
+ import { AuthUIContext } from "../../lib/auth-ui-provider"
5
+ import { cn } from "../../lib/utils"
6
+ import type { AuthLocalization } from "../../localization/auth-localization"
7
+ import { DeleteAccountCard } from "./account/delete-account-card"
8
+ import { PasskeysCard } from "./passkey/passkeys-card"
9
+ import { ProvidersCard } from "./providers/providers-card"
10
+ import { ChangePasswordCard } from "./security/change-password-card"
11
+ import { SessionsCard } from "./security/sessions-card"
12
+ import type { SettingsCardClassNames } from "./shared/settings-card"
13
+ import { TwoFactorCard } from "./two-factor/two-factor-card"
14
+
15
+ export function SecuritySettingsCards({
16
+ className,
17
+ classNames,
18
+ localization
19
+ }: {
20
+ className?: string
21
+ classNames?: {
22
+ card?: SettingsCardClassNames
23
+ cards?: string
24
+ }
25
+ localization?: Partial<AuthLocalization>
26
+ }) {
27
+ const {
28
+ credentials,
29
+ deleteUser,
30
+ hooks,
31
+ localization: contextLocalization,
32
+ passkey,
33
+ social,
34
+ genericOAuth,
35
+ twoFactor
36
+ } = useContext(AuthUIContext)
37
+
38
+ localization = { ...contextLocalization, ...localization }
39
+
40
+ const { useListAccounts } = hooks
41
+
42
+ const {
43
+ data: accounts,
44
+ isPending: accountsPending,
45
+ refetch: refetchAccounts
46
+ } = useListAccounts()
47
+
48
+ const credentialsLinked = accounts?.some(
49
+ (acc) => acc.providerId === "credential"
50
+ )
51
+
52
+ return (
53
+ <div
54
+ className={cn(
55
+ "flex w-full flex-col gap-4 md:gap-6",
56
+ className,
57
+ classNames?.cards
58
+ )}
59
+ >
60
+ {credentials && (
61
+ <ChangePasswordCard
62
+ accounts={accounts}
63
+ classNames={classNames?.card}
64
+ isPending={accountsPending}
65
+ localization={localization}
66
+ skipHook
67
+ />
68
+ )}
69
+
70
+ {(social?.providers?.length || genericOAuth?.providers?.length) && (
71
+ <ProvidersCard
72
+ accounts={accounts}
73
+ classNames={classNames?.card}
74
+ isPending={accountsPending}
75
+ localization={localization}
76
+ refetch={refetchAccounts}
77
+ skipHook
78
+ />
79
+ )}
80
+
81
+ {twoFactor && credentialsLinked && (
82
+ <TwoFactorCard
83
+ classNames={classNames?.card}
84
+ localization={localization}
85
+ />
86
+ )}
87
+
88
+ {passkey && (
89
+ <PasskeysCard
90
+ classNames={classNames?.card}
91
+ localization={localization}
92
+ />
93
+ )}
94
+
95
+ <SessionsCard
96
+ classNames={classNames?.card}
97
+ localization={localization}
98
+ />
99
+
100
+ {deleteUser && (
101
+ <DeleteAccountCard
102
+ accounts={accounts}
103
+ classNames={classNames?.card}
104
+ isPending={accountsPending}
105
+ localization={localization}
106
+ skipHook
107
+ />
108
+ )}
109
+ </div>
110
+ )
111
+ }