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,288 @@
1
+ "use client"
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod"
4
+ import { Loader2 } from "lucide-react"
5
+ import { useContext, useEffect, useState } from "react"
6
+ import { useForm } from "react-hook-form"
7
+ import * as z from "zod"
8
+
9
+ import { useIsHydrated } from "../../../hooks/use-hydrated"
10
+ import { useOnSuccessTransition } from "../../../hooks/use-success-transition"
11
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
12
+ import { cn, getLocalizedError } from "../../../lib/utils"
13
+ import type { AuthLocalization } from "../../../localization/auth-localization"
14
+ import { Button } from "../../ui/button"
15
+ import {
16
+ Form,
17
+ FormControl,
18
+ FormField,
19
+ FormItem,
20
+ FormLabel,
21
+ FormMessage
22
+ } from "../../ui/form"
23
+ import { Input } from "../../ui/input"
24
+ import { InputOTP } from "../../ui/input-otp"
25
+ import type { AuthFormClassNames } from "../auth-form"
26
+ import { OTPInputGroup } from "../otp-input-group"
27
+
28
+ export interface EmailOTPFormProps {
29
+ className?: string
30
+ classNames?: AuthFormClassNames
31
+ callbackURL?: string
32
+ isSubmitting?: boolean
33
+ localization: Partial<AuthLocalization>
34
+ otpSeparators?: 0 | 1 | 2
35
+ redirectTo?: string
36
+ setIsSubmitting?: (value: boolean) => void
37
+ }
38
+
39
+ export function EmailOTPForm(props: EmailOTPFormProps) {
40
+ const [email, setEmail] = useState<string | undefined>()
41
+
42
+ if (!email) {
43
+ return <EmailForm {...props} setEmail={setEmail} />
44
+ }
45
+
46
+ return <OTPForm {...props} email={email} />
47
+ }
48
+
49
+ function EmailForm({
50
+ className,
51
+ classNames,
52
+ isSubmitting,
53
+ localization,
54
+ setIsSubmitting,
55
+ setEmail
56
+ }: EmailOTPFormProps & {
57
+ setEmail: (email: string) => void
58
+ }) {
59
+ const isHydrated = useIsHydrated()
60
+
61
+ const {
62
+ authClient,
63
+ localization: contextLocalization,
64
+ toast
65
+ } = useContext(AuthUIContext)
66
+
67
+ localization = { ...contextLocalization, ...localization }
68
+
69
+ const formSchema = z.object({
70
+ email: z.string().email({
71
+ message: `${localization.EMAIL} ${localization.IS_INVALID}`
72
+ })
73
+ })
74
+
75
+ const form = useForm({
76
+ resolver: zodResolver(formSchema),
77
+ defaultValues: {
78
+ email: ""
79
+ }
80
+ })
81
+
82
+ isSubmitting = isSubmitting || form.formState.isSubmitting
83
+
84
+ useEffect(() => {
85
+ setIsSubmitting?.(form.formState.isSubmitting)
86
+ }, [form.formState.isSubmitting, setIsSubmitting])
87
+
88
+ async function sendEmailOTP({ email }: z.infer<typeof formSchema>) {
89
+ try {
90
+ await authClient.emailOtp.sendVerificationOtp({
91
+ email,
92
+ type: "sign-in",
93
+ fetchOptions: { throw: true }
94
+ })
95
+
96
+ toast({
97
+ variant: "success",
98
+ message: localization.EMAIL_OTP_VERIFICATION_SENT
99
+ })
100
+
101
+ setEmail(email)
102
+ } catch (error) {
103
+ toast({
104
+ variant: "error",
105
+ message: getLocalizedError({ error, localization })
106
+ })
107
+ }
108
+ }
109
+
110
+ return (
111
+ <Form {...form}>
112
+ <form
113
+ onSubmit={form.handleSubmit(sendEmailOTP)}
114
+ noValidate={isHydrated}
115
+ className={cn("grid w-full gap-6", className, classNames?.base)}
116
+ >
117
+ <FormField
118
+ control={form.control}
119
+ name="email"
120
+ render={({ field }) => (
121
+ <FormItem>
122
+ <FormLabel className={classNames?.label}>
123
+ {localization.EMAIL}
124
+ </FormLabel>
125
+
126
+ <FormControl>
127
+ <Input
128
+ className={classNames?.input}
129
+ type="email"
130
+ placeholder={localization.EMAIL_PLACEHOLDER}
131
+ disabled={isSubmitting}
132
+ {...field}
133
+ />
134
+ </FormControl>
135
+
136
+ <FormMessage className={classNames?.error} />
137
+ </FormItem>
138
+ )}
139
+ />
140
+
141
+ <Button
142
+ type="submit"
143
+ disabled={isSubmitting}
144
+ className={cn(
145
+ "w-full",
146
+ classNames?.button,
147
+ classNames?.primaryButton
148
+ )}
149
+ >
150
+ {isSubmitting ? (
151
+ <Loader2 className="animate-spin" />
152
+ ) : (
153
+ localization.EMAIL_OTP_SEND_ACTION
154
+ )}
155
+ </Button>
156
+ </form>
157
+ </Form>
158
+ )
159
+ }
160
+
161
+ export function OTPForm({
162
+ className,
163
+ classNames,
164
+ isSubmitting,
165
+ localization,
166
+ otpSeparators = 0,
167
+ redirectTo,
168
+ setIsSubmitting,
169
+ email
170
+ }: EmailOTPFormProps & {
171
+ email: string
172
+ }) {
173
+ const {
174
+ authClient,
175
+ localization: contextLocalization,
176
+ toast
177
+ } = useContext(AuthUIContext)
178
+
179
+ localization = { ...contextLocalization, ...localization }
180
+
181
+ const { onSuccess, isPending: transitionPending } = useOnSuccessTransition({
182
+ redirectTo
183
+ })
184
+
185
+ const formSchema = z.object({
186
+ code: z
187
+ .string()
188
+ .min(1, {
189
+ message: `${localization.EMAIL_OTP} ${localization.IS_REQUIRED}`
190
+ })
191
+ .min(6, {
192
+ message: `${localization.EMAIL_OTP} ${localization.IS_INVALID}`
193
+ })
194
+ })
195
+
196
+ const form = useForm({
197
+ resolver: zodResolver(formSchema),
198
+ defaultValues: {
199
+ code: ""
200
+ }
201
+ })
202
+
203
+ isSubmitting =
204
+ isSubmitting || form.formState.isSubmitting || transitionPending
205
+
206
+ useEffect(() => {
207
+ setIsSubmitting?.(form.formState.isSubmitting || transitionPending)
208
+ }, [form.formState.isSubmitting, transitionPending, setIsSubmitting])
209
+
210
+ async function verifyCode({ code }: z.infer<typeof formSchema>) {
211
+ try {
212
+ await authClient.signIn.emailOtp({
213
+ email,
214
+ otp: code,
215
+ fetchOptions: { throw: true }
216
+ })
217
+
218
+ await onSuccess()
219
+ } catch (error) {
220
+ toast({
221
+ variant: "error",
222
+ message: getLocalizedError({ error, localization })
223
+ })
224
+
225
+ form.reset()
226
+ }
227
+ }
228
+
229
+ return (
230
+ <Form {...form}>
231
+ <form
232
+ onSubmit={form.handleSubmit(verifyCode)}
233
+ className={cn("grid w-full gap-6", className, classNames?.base)}
234
+ >
235
+ <FormField
236
+ control={form.control}
237
+ name="code"
238
+ render={({ field }) => (
239
+ <FormItem>
240
+ <FormLabel className={classNames?.label}>
241
+ {localization.EMAIL_OTP}
242
+ </FormLabel>
243
+
244
+ <FormControl>
245
+ <InputOTP
246
+ {...field}
247
+ maxLength={6}
248
+ onChange={(value) => {
249
+ field.onChange(value)
250
+
251
+ if (value.length === 6) {
252
+ form.handleSubmit(verifyCode)()
253
+ }
254
+ }}
255
+ containerClassName={
256
+ classNames?.otpInputContainer
257
+ }
258
+ className={classNames?.otpInput}
259
+ disabled={isSubmitting}
260
+ >
261
+ <OTPInputGroup
262
+ otpSeparators={otpSeparators}
263
+ />
264
+ </InputOTP>
265
+ </FormControl>
266
+
267
+ <FormMessage className={classNames?.error} />
268
+ </FormItem>
269
+ )}
270
+ />
271
+
272
+ <div className="grid gap-4">
273
+ <Button
274
+ type="submit"
275
+ disabled={isSubmitting}
276
+ className={cn(
277
+ classNames?.button,
278
+ classNames?.primaryButton
279
+ )}
280
+ >
281
+ {isSubmitting && <Loader2 className="animate-spin" />}
282
+ {localization.EMAIL_OTP_VERIFY_ACTION}
283
+ </Button>
284
+ </div>
285
+ </form>
286
+ </Form>
287
+ )
288
+ }
@@ -0,0 +1,168 @@
1
+ "use client"
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod"
4
+ import type { BetterFetchOption } from "better-auth/react"
5
+ import { Loader2 } from "lucide-react"
6
+ import { useContext, useEffect } from "react"
7
+ import { useForm } from "react-hook-form"
8
+ import * as z from "zod"
9
+ import { useCaptcha } from "../../../hooks/use-captcha"
10
+ import { useIsHydrated } from "../../../hooks/use-hydrated"
11
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
12
+ import { cn, getLocalizedError } from "../../../lib/utils"
13
+ import type { AuthLocalization } from "../../../localization/auth-localization"
14
+ import { Captcha } from "../../captcha/captcha"
15
+ import { Button } from "../../ui/button"
16
+ import {
17
+ Form,
18
+ FormControl,
19
+ FormField,
20
+ FormItem,
21
+ FormLabel,
22
+ FormMessage
23
+ } from "../../ui/form"
24
+ import { Input } from "../../ui/input"
25
+ import type { AuthFormClassNames } from "../auth-form"
26
+
27
+ export interface ForgotPasswordFormProps {
28
+ className?: string
29
+ classNames?: AuthFormClassNames
30
+ isSubmitting?: boolean
31
+ localization: Partial<AuthLocalization>
32
+ setIsSubmitting?: (value: boolean) => void
33
+ }
34
+
35
+ export function ForgotPasswordForm({
36
+ className,
37
+ classNames,
38
+ isSubmitting,
39
+ localization,
40
+ setIsSubmitting
41
+ }: ForgotPasswordFormProps) {
42
+ const isHydrated = useIsHydrated()
43
+ const { captchaRef, getCaptchaHeaders, resetCaptcha } = useCaptcha({
44
+ localization
45
+ })
46
+
47
+ const {
48
+ authClient,
49
+ basePath,
50
+ baseURL,
51
+ localization: contextLocalization,
52
+ navigate,
53
+ toast,
54
+ viewPaths
55
+ } = useContext(AuthUIContext)
56
+
57
+ localization = { ...contextLocalization, ...localization }
58
+
59
+ const formSchema = z.object({
60
+ email: z
61
+ .string()
62
+ .email({
63
+ message: `${localization.EMAIL} ${localization.IS_INVALID}`
64
+ })
65
+ .min(1, {
66
+ message: `${localization.EMAIL} ${localization.IS_REQUIRED}`
67
+ })
68
+ })
69
+
70
+ const form = useForm({
71
+ resolver: zodResolver(formSchema),
72
+ defaultValues: {
73
+ email: ""
74
+ }
75
+ })
76
+
77
+ isSubmitting = isSubmitting || form.formState.isSubmitting
78
+
79
+ useEffect(() => {
80
+ setIsSubmitting?.(form.formState.isSubmitting)
81
+ }, [form.formState.isSubmitting, setIsSubmitting])
82
+
83
+ async function forgotPassword({ email }: z.infer<typeof formSchema>) {
84
+ try {
85
+ const fetchOptions: BetterFetchOption = {
86
+ throw: true,
87
+ headers: await getCaptchaHeaders("/forget-password")
88
+ }
89
+
90
+ await authClient.requestPasswordReset({
91
+ email,
92
+ redirectTo: `${baseURL}${basePath}/${viewPaths.RESET_PASSWORD}`,
93
+ fetchOptions
94
+ })
95
+
96
+ toast({
97
+ variant: "success",
98
+ message: localization.FORGOT_PASSWORD_EMAIL
99
+ })
100
+
101
+ navigate(
102
+ `${basePath}/${viewPaths.SIGN_IN}${window.location.search}`
103
+ )
104
+ } catch (error) {
105
+ toast({
106
+ variant: "error",
107
+ message: getLocalizedError({ error, localization })
108
+ })
109
+ resetCaptcha()
110
+ }
111
+ }
112
+
113
+ return (
114
+ <Form {...form}>
115
+ <form
116
+ onSubmit={form.handleSubmit(forgotPassword)}
117
+ noValidate={isHydrated}
118
+ className={cn("grid w-full gap-6", className, classNames?.base)}
119
+ >
120
+ <FormField
121
+ control={form.control}
122
+ name="email"
123
+ render={({ field }) => (
124
+ <FormItem>
125
+ <FormLabel className={classNames?.label}>
126
+ {localization.EMAIL}
127
+ </FormLabel>
128
+
129
+ <FormControl>
130
+ <Input
131
+ className={classNames?.input}
132
+ type="email"
133
+ placeholder={localization.EMAIL_PLACEHOLDER}
134
+ disabled={isSubmitting}
135
+ {...field}
136
+ />
137
+ </FormControl>
138
+
139
+ <FormMessage className={classNames?.error} />
140
+ </FormItem>
141
+ )}
142
+ />
143
+
144
+ <Captcha
145
+ ref={captchaRef}
146
+ localization={localization}
147
+ action="/forget-password"
148
+ />
149
+
150
+ <Button
151
+ type="submit"
152
+ disabled={isSubmitting}
153
+ className={cn(
154
+ "w-full",
155
+ classNames?.button,
156
+ classNames?.primaryButton
157
+ )}
158
+ >
159
+ {isSubmitting ? (
160
+ <Loader2 className="animate-spin" />
161
+ ) : (
162
+ localization.FORGOT_PASSWORD_ACTION
163
+ )}
164
+ </Button>
165
+ </form>
166
+ </Form>
167
+ )
168
+ }
@@ -0,0 +1,191 @@
1
+ "use client"
2
+
3
+ import { zodResolver } from "@hookform/resolvers/zod"
4
+ import type { BetterFetchOption } from "better-auth/react"
5
+ import { Loader2 } from "lucide-react"
6
+ import { useCallback, useContext, useEffect } from "react"
7
+ import { useForm } from "react-hook-form"
8
+ import * as z from "zod"
9
+
10
+ import { useCaptcha } from "../../../hooks/use-captcha"
11
+ import { useIsHydrated } from "../../../hooks/use-hydrated"
12
+ import { AuthUIContext } from "../../../lib/auth-ui-provider"
13
+ import { cn, getLocalizedError, getSearchParam } from "../../../lib/utils"
14
+ import type { AuthLocalization } from "../../../localization/auth-localization"
15
+ import { Captcha } from "../../captcha/captcha"
16
+ import { Button } from "../../ui/button"
17
+ import {
18
+ Form,
19
+ FormControl,
20
+ FormField,
21
+ FormItem,
22
+ FormLabel,
23
+ FormMessage
24
+ } from "../../ui/form"
25
+ import { Input } from "../../ui/input"
26
+ import type { AuthFormClassNames } from "../auth-form"
27
+
28
+ export interface MagicLinkFormProps {
29
+ className?: string
30
+ classNames?: AuthFormClassNames
31
+ callbackURL?: string
32
+ isSubmitting?: boolean
33
+ localization: Partial<AuthLocalization>
34
+ redirectTo?: string
35
+ setIsSubmitting?: (value: boolean) => void
36
+ }
37
+
38
+ export function MagicLinkForm({
39
+ className,
40
+ classNames,
41
+ callbackURL: callbackURLProp,
42
+ isSubmitting,
43
+ localization,
44
+ redirectTo: redirectToProp,
45
+ setIsSubmitting
46
+ }: MagicLinkFormProps) {
47
+ const isHydrated = useIsHydrated()
48
+ const { captchaRef, getCaptchaHeaders, resetCaptcha } = useCaptcha({
49
+ localization
50
+ })
51
+
52
+ const {
53
+ authClient,
54
+ basePath,
55
+ baseURL,
56
+ persistClient,
57
+ localization: contextLocalization,
58
+ redirectTo: contextRedirectTo,
59
+ viewPaths,
60
+ toast
61
+ } = useContext(AuthUIContext)
62
+
63
+ localization = { ...contextLocalization, ...localization }
64
+
65
+ const getRedirectTo = useCallback(
66
+ () =>
67
+ redirectToProp || getSearchParam("redirectTo") || contextRedirectTo,
68
+ [redirectToProp, contextRedirectTo]
69
+ )
70
+
71
+ const getCallbackURL = useCallback(
72
+ () =>
73
+ `${baseURL}${
74
+ callbackURLProp ||
75
+ (persistClient
76
+ ? `${basePath}/${viewPaths.CALLBACK}?redirectTo=${getRedirectTo()}`
77
+ : getRedirectTo())
78
+ }`,
79
+ [
80
+ callbackURLProp,
81
+ persistClient,
82
+ basePath,
83
+ viewPaths,
84
+ baseURL,
85
+ getRedirectTo
86
+ ]
87
+ )
88
+
89
+ const formSchema = z.object({
90
+ email: z.string().email({
91
+ message: `${localization.EMAIL} ${localization.IS_INVALID}`
92
+ })
93
+ })
94
+
95
+ const form = useForm({
96
+ resolver: zodResolver(formSchema),
97
+ defaultValues: {
98
+ email: ""
99
+ }
100
+ })
101
+
102
+ isSubmitting = isSubmitting || form.formState.isSubmitting
103
+
104
+ useEffect(() => {
105
+ setIsSubmitting?.(form.formState.isSubmitting)
106
+ }, [form.formState.isSubmitting, setIsSubmitting])
107
+
108
+ async function sendMagicLink({ email }: z.infer<typeof formSchema>) {
109
+ try {
110
+ const fetchOptions: BetterFetchOption = {
111
+ throw: true,
112
+ headers: await getCaptchaHeaders("/sign-in/magic-link")
113
+ }
114
+
115
+ await authClient.signIn.magicLink({
116
+ email,
117
+ callbackURL: getCallbackURL(),
118
+ fetchOptions
119
+ })
120
+
121
+ toast({
122
+ variant: "success",
123
+ message: localization.MAGIC_LINK_EMAIL
124
+ })
125
+
126
+ form.reset()
127
+ } catch (error) {
128
+ toast({
129
+ variant: "error",
130
+ message: getLocalizedError({ error, localization })
131
+ })
132
+ resetCaptcha()
133
+ }
134
+ }
135
+
136
+ return (
137
+ <Form {...form}>
138
+ <form
139
+ onSubmit={form.handleSubmit(sendMagicLink)}
140
+ noValidate={isHydrated}
141
+ className={cn("grid w-full gap-6", className, classNames?.base)}
142
+ >
143
+ <FormField
144
+ control={form.control}
145
+ name="email"
146
+ render={({ field }) => (
147
+ <FormItem>
148
+ <FormLabel className={classNames?.label}>
149
+ {localization.EMAIL}
150
+ </FormLabel>
151
+
152
+ <FormControl>
153
+ <Input
154
+ className={classNames?.input}
155
+ type="email"
156
+ placeholder={localization.EMAIL_PLACEHOLDER}
157
+ disabled={isSubmitting}
158
+ {...field}
159
+ />
160
+ </FormControl>
161
+
162
+ <FormMessage className={classNames?.error} />
163
+ </FormItem>
164
+ )}
165
+ />
166
+
167
+ <Captcha
168
+ ref={captchaRef}
169
+ localization={localization}
170
+ action="/sign-in/magic-link"
171
+ />
172
+
173
+ <Button
174
+ type="submit"
175
+ disabled={isSubmitting}
176
+ className={cn(
177
+ "w-full",
178
+ classNames?.button,
179
+ classNames?.primaryButton
180
+ )}
181
+ >
182
+ {isSubmitting ? (
183
+ <Loader2 className="animate-spin" />
184
+ ) : (
185
+ localization.MAGIC_LINK_ACTION
186
+ )}
187
+ </Button>
188
+ </form>
189
+ </Form>
190
+ )
191
+ }