@better-auth-ui/core 1.6.0

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 (97) hide show
  1. package/dist/base-paths.d.ts +21 -0
  2. package/dist/base-paths.js +5 -0
  3. package/dist/config/appearance-config.d.ts +23 -0
  4. package/dist/config/appearance-config.js +1 -0
  5. package/dist/config/auth-config.d.ts +87 -0
  6. package/dist/config/auth-config.js +35 -0
  7. package/dist/config/avatar-config.d.ts +35 -0
  8. package/dist/config/avatar-config.js +1 -0
  9. package/dist/config/delete-user-config.d.ts +14 -0
  10. package/dist/config/delete-user-config.js +1 -0
  11. package/dist/config/email-and-password-config.d.ts +37 -0
  12. package/dist/config/email-and-password-config.js +1 -0
  13. package/dist/config/index.d.ts +5 -0
  14. package/dist/config/index.js +5 -0
  15. package/dist/config/settings-config.d.ts +17 -0
  16. package/dist/config/settings-config.js +1 -0
  17. package/dist/config/toast-config.d.ts +58 -0
  18. package/dist/config/toast-config.js +16 -0
  19. package/dist/index.d.ts +6 -0
  20. package/dist/index.js +6 -0
  21. package/dist/lib/auth-callback-options.d.ts +8 -0
  22. package/dist/lib/auth-callback-options.js +1 -0
  23. package/dist/lib/auth-config.d.ts +198 -0
  24. package/dist/lib/auth-config.js +36 -0
  25. package/dist/lib/auth-error.d.ts +9 -0
  26. package/dist/lib/auth-error.js +1 -0
  27. package/dist/lib/auth-hook-options.d.ts +8 -0
  28. package/dist/lib/auth-hook-options.js +1 -0
  29. package/dist/lib/auth-toast.d.ts +24 -0
  30. package/dist/lib/auth-toast.js +10 -0
  31. package/dist/lib/base-paths.d.ts +21 -0
  32. package/dist/lib/base-paths.js +5 -0
  33. package/dist/lib/config/appearance-config.d.ts +23 -0
  34. package/dist/lib/config/appearance-config.js +1 -0
  35. package/dist/lib/config/auth-config.d.ts +70 -0
  36. package/dist/lib/config/auth-config.js +36 -0
  37. package/dist/lib/config/avatar-config.d.ts +35 -0
  38. package/dist/lib/config/avatar-config.js +1 -0
  39. package/dist/lib/config/email-and-password-config.d.ts +37 -0
  40. package/dist/lib/config/email-and-password-config.js +1 -0
  41. package/dist/lib/config/index.d.ts +6 -0
  42. package/dist/lib/config/index.js +6 -0
  43. package/dist/lib/config/settings-config.d.ts +22 -0
  44. package/dist/lib/config/settings-config.js +1 -0
  45. package/dist/lib/config/toast-config.d.ts +24 -0
  46. package/dist/lib/config/toast-config.js +10 -0
  47. package/dist/lib/localization.d.ts +175 -0
  48. package/dist/lib/localization.js +172 -0
  49. package/dist/lib/provider-names.d.ts +12 -0
  50. package/dist/lib/provider-names.js +50 -0
  51. package/dist/lib/toast-config.d.ts +24 -0
  52. package/dist/lib/toast-config.js +10 -0
  53. package/dist/lib/utils.d.ts +32 -0
  54. package/dist/lib/utils.js +116 -0
  55. package/dist/lib/view-paths.d.ts +68 -0
  56. package/dist/lib/view-paths.js +14 -0
  57. package/dist/localization/index.d.ts +35 -0
  58. package/dist/localization/index.js +34 -0
  59. package/dist/localization/localization.d.ts +35 -0
  60. package/dist/localization/localization.js +34 -0
  61. package/dist/localization.d.ts +159 -0
  62. package/dist/localization.js +156 -0
  63. package/dist/provider-names.d.ts +12 -0
  64. package/dist/provider-names.js +50 -0
  65. package/dist/src/index.d.ts +7 -0
  66. package/dist/src/index.js +7 -0
  67. package/dist/src/lib/auth-config.d.ts +148 -0
  68. package/dist/src/lib/auth-config.js +28 -0
  69. package/dist/src/lib/auth-toast.d.ts +24 -0
  70. package/dist/src/lib/auth-toast.js +10 -0
  71. package/dist/src/lib/base-paths.d.ts +9 -0
  72. package/dist/src/lib/base-paths.js +9 -0
  73. package/dist/src/lib/localization.d.ts +145 -0
  74. package/dist/src/lib/localization.js +142 -0
  75. package/dist/src/lib/provider-names.d.ts +12 -0
  76. package/dist/src/lib/provider-names.js +49 -0
  77. package/dist/src/lib/utils.d.ts +1 -0
  78. package/dist/src/lib/utils.js +30 -0
  79. package/dist/src/lib/view-paths.d.ts +30 -0
  80. package/dist/src/lib/view-paths.js +17 -0
  81. package/dist/utils.d.ts +3 -0
  82. package/dist/utils.js +80 -0
  83. package/dist/view-paths.d.ts +30 -0
  84. package/dist/view-paths.js +17 -0
  85. package/package.json +32 -0
  86. package/src/config/appearance-config.ts +24 -0
  87. package/src/config/auth-config.ts +117 -0
  88. package/src/config/avatar-config.ts +39 -0
  89. package/src/config/delete-user-config.ts +14 -0
  90. package/src/config/email-and-password-config.ts +37 -0
  91. package/src/config/index.ts +5 -0
  92. package/src/index.ts +6 -0
  93. package/src/lib/base-paths.ts +26 -0
  94. package/src/lib/localization.ts +259 -0
  95. package/src/lib/provider-names.ts +55 -0
  96. package/src/lib/utils.ts +151 -0
  97. package/src/lib/view-paths.ts +86 -0
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Type guard that checks whether a value is a non-null, non-array object.
3
+ */
4
+ function isObject(item: unknown): item is Record<string, unknown> {
5
+ return item !== null && typeof item === "object" && !Array.isArray(item)
6
+ }
7
+
8
+ /**
9
+ * Type guard that checks whether a value is a plain object suitable for deep merging.
10
+ * Excludes `Date`, `RegExp`, and other special object types.
11
+ */
12
+ function isPlainObject(item: unknown): item is Record<string, unknown> {
13
+ if (!isObject(item)) return false
14
+
15
+ // Handle special object types that should not be merged
16
+ if (item instanceof Date) return false
17
+ if (item instanceof RegExp) return false
18
+
19
+ return true
20
+ }
21
+
22
+ /**
23
+ * Resize and square-crop an image file for use as an avatar.
24
+ *
25
+ * The image is center-cropped to a square, scaled down to at most {@link size} pixels,
26
+ * and converted to the specified output format.
27
+ *
28
+ * @param file - The source image file.
29
+ * @param size - Max dimension in pixels for the output image.
30
+ * @param extension - Output format. Use `"inherit"` to keep the original format.
31
+ * @returns A promise that resolves to the processed `File`.
32
+ */
33
+ export function resizeAvatar(
34
+ file: File,
35
+ size = 256,
36
+ extension: "png" | "jpg" | "webp" | "inherit" = "png"
37
+ ): Promise<File> {
38
+ const MAX_SIZE = size
39
+ const resolvedExtension =
40
+ extension === "inherit" ? file.name.split(".").pop() : extension
41
+ const mimeType =
42
+ extension === "inherit"
43
+ ? file.type
44
+ : `image/${extension === "jpg" ? "jpeg" : extension}`
45
+
46
+ return new Promise((resolve, reject) => {
47
+ const img = new Image()
48
+ const url = URL.createObjectURL(file)
49
+
50
+ img.onload = () => {
51
+ URL.revokeObjectURL(url)
52
+
53
+ const { naturalWidth: w, naturalHeight: h } = img
54
+ const side = Math.min(w, h)
55
+
56
+ const cropX = (w - side) / 2
57
+ const cropY = (h - side) / 2
58
+
59
+ const outSize = Math.min(side, MAX_SIZE)
60
+
61
+ const canvas = document.createElement("canvas")
62
+ canvas.width = outSize
63
+ canvas.height = outSize
64
+
65
+ const ctx = canvas.getContext("2d")
66
+ if (!ctx) {
67
+ reject(new Error("Could not get canvas context"))
68
+ return
69
+ }
70
+
71
+ ctx.drawImage(img, cropX, cropY, side, side, 0, 0, outSize, outSize)
72
+
73
+ canvas.toBlob(
74
+ (blob) => {
75
+ if (!blob) {
76
+ reject(new Error("Could not create blob from canvas"))
77
+ return
78
+ }
79
+ resolve(
80
+ new File(
81
+ [blob],
82
+ file.name.replace(/\.[^.]+$/, `.${resolvedExtension}`),
83
+ {
84
+ type: mimeType
85
+ }
86
+ )
87
+ )
88
+ },
89
+ mimeType,
90
+ 1
91
+ )
92
+ }
93
+
94
+ img.onerror = () => {
95
+ URL.revokeObjectURL(url)
96
+ reject(new Error("Failed to load image"))
97
+ }
98
+
99
+ img.src = url
100
+ })
101
+ }
102
+
103
+ /**
104
+ * Convert a `File` to a base64-encoded data URL string.
105
+ *
106
+ * @param file - The file to encode.
107
+ * @returns A promise that resolves to the base64 data URL.
108
+ */
109
+ export function fileToBase64(file: File): Promise<string> {
110
+ return new Promise((resolve, reject) => {
111
+ const reader = new FileReader()
112
+ reader.onload = () => resolve(reader.result as string)
113
+ reader.onerror = () => reject(new Error("Failed to read file"))
114
+ reader.readAsDataURL(file)
115
+ })
116
+ }
117
+
118
+ /**
119
+ * Recursively merge `source` into `target`, producing a new object.
120
+ *
121
+ * - Plain objects are merged key-by-key; nested objects are merged recursively.
122
+ * - `undefined` values in `source` are skipped (existing `target` values are preserved).
123
+ * - Non-plain values (arrays, `Date`, `RegExp`, primitives, functions) in `source`
124
+ * replace the corresponding `target` value outright.
125
+ *
126
+ * @param target - The base object.
127
+ * @param source - Partial overrides to apply on top of `target`.
128
+ * @returns A new merged object of type `T`.
129
+ */
130
+ export function deepmerge<T>(target: T, source: Partial<T>): T {
131
+ if (isPlainObject(target) && isPlainObject(source)) {
132
+ const result: Record<string, unknown> = { ...target }
133
+
134
+ for (const [key, value] of Object.entries(source)) {
135
+ if (value === undefined) continue // skip undefineds
136
+
137
+ if (key in target) {
138
+ result[key] = deepmerge(
139
+ (target as Record<string, unknown>)[key],
140
+ value as unknown as Partial<T>
141
+ )
142
+ } else {
143
+ result[key] = value
144
+ }
145
+ }
146
+
147
+ return result as T
148
+ }
149
+
150
+ return source as T
151
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * View path segments for authentication routes.
3
+ */
4
+ export type AuthViewPaths = {
5
+ /**
6
+ * Path segment for the sign-in view
7
+ * @default "sign-in"
8
+ */
9
+ signIn: string
10
+ /**
11
+ * Path segment for the sign-up view
12
+ * @default "sign-up"
13
+ */
14
+ signUp: string
15
+ /**
16
+ * Path segment for the magic link authentication view
17
+ * @default "magic-link"
18
+ */
19
+ magicLink: string
20
+ /**
21
+ * Path segment for the forgot password view
22
+ * @default "forgot-password"
23
+ */
24
+ forgotPassword: string
25
+ /**
26
+ * Path segment for the reset password view
27
+ * @default "reset-password"
28
+ */
29
+ resetPassword: string
30
+ /**
31
+ * Path segment for the sign-out view
32
+ * @default "sign-out"
33
+ */
34
+ signOut: string
35
+ }
36
+
37
+ /**
38
+ * View path segments for settings routes.
39
+ */
40
+ export type SettingsViewPaths = {
41
+ /**
42
+ * Path segment for the account settings view
43
+ * @default "account"
44
+ */
45
+ account: string
46
+ /**
47
+ * Path segment for the security settings view
48
+ * @default "security"
49
+ */
50
+ security: string
51
+ }
52
+
53
+ /**
54
+ * View path configuration for authentication and settings routes.
55
+ */
56
+ export type ViewPaths = {
57
+ /** Auth view path segments */
58
+ auth: AuthViewPaths
59
+ /** Settings view path segments */
60
+ settings: SettingsViewPaths
61
+ }
62
+
63
+ export const viewPaths: ViewPaths = {
64
+ auth: {
65
+ signIn: "sign-in",
66
+ signUp: "sign-up",
67
+ magicLink: "magic-link",
68
+ forgotPassword: "forgot-password",
69
+ resetPassword: "reset-password",
70
+ signOut: "sign-out"
71
+ },
72
+ settings: {
73
+ account: "account",
74
+ security: "security"
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Valid auth view key.
80
+ */
81
+ export type AuthView = keyof AuthViewPaths
82
+
83
+ /**
84
+ * Valid settings view key.
85
+ */
86
+ export type SettingsView = keyof SettingsViewPaths