@lupinum/better-convex-nuxt 0.8.0-beta.40

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 (268) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSES/Apache-2.0.txt +201 -0
  3. package/README.md +151 -0
  4. package/THIRD_PARTY_NOTICES.md +38 -0
  5. package/dist/module.d.mts +55 -0
  6. package/dist/module.json +12 -0
  7. package/dist/module.mjs +309 -0
  8. package/dist/runtime/auth/auth-identity.d.ts +31 -0
  9. package/dist/runtime/auth/auth-identity.js +21 -0
  10. package/dist/runtime/auth/better-auth-browser-adapter.d.ts +33 -0
  11. package/dist/runtime/auth/better-auth-browser-adapter.js +215 -0
  12. package/dist/runtime/auth/client-engine-types.d.ts +4 -0
  13. package/dist/runtime/auth/client-engine-types.js +0 -0
  14. package/dist/runtime/auth/integrated-client.d.ts +25 -0
  15. package/dist/runtime/auth/integrated-client.js +132 -0
  16. package/dist/runtime/auth/operation-tracker.d.ts +7 -0
  17. package/dist/runtime/auth/operation-tracker.js +14 -0
  18. package/dist/runtime/auth/session-synchronization.d.ts +23 -0
  19. package/dist/runtime/auth/session-synchronization.js +156 -0
  20. package/dist/runtime/auth/token-fetcher.d.ts +66 -0
  21. package/dist/runtime/auth/token-fetcher.js +131 -0
  22. package/dist/runtime/auth/validate-auth-client-definition.d.ts +12 -0
  23. package/dist/runtime/auth/validate-auth-client-definition.js +47 -0
  24. package/dist/runtime/auth-client/convex-client-plugin.d.ts +31 -0
  25. package/dist/runtime/auth-client/convex-client-plugin.js +14 -0
  26. package/dist/runtime/auth-client/definition-types.d.ts +6 -0
  27. package/dist/runtime/auth-client/definition-types.js +0 -0
  28. package/dist/runtime/auth-client/empty-definition.d.ts +2 -0
  29. package/dist/runtime/auth-client/empty-definition.js +2 -0
  30. package/dist/runtime/auth-client/index.d.ts +71 -0
  31. package/dist/runtime/auth-client/index.js +3 -0
  32. package/dist/runtime/cli/auth-schema.d.ts +2 -0
  33. package/dist/runtime/cli/auth-schema.js +169 -0
  34. package/dist/runtime/cli/convex.d.ts +11 -0
  35. package/dist/runtime/cli/convex.js +274 -0
  36. package/dist/runtime/composables/useConvex.d.ts +4 -0
  37. package/dist/runtime/composables/useConvex.js +9 -0
  38. package/dist/runtime/composables/useConvexAction.d.ts +4 -0
  39. package/dist/runtime/composables/useConvexAction.js +18 -0
  40. package/dist/runtime/composables/useConvexAttachment.d.ts +9 -0
  41. package/dist/runtime/composables/useConvexAttachment.js +14 -0
  42. package/dist/runtime/composables/useConvexAuth.d.ts +10 -0
  43. package/dist/runtime/composables/useConvexAuth.js +37 -0
  44. package/dist/runtime/composables/useConvexConfig.d.ts +12 -0
  45. package/dist/runtime/composables/useConvexConfig.js +7 -0
  46. package/dist/runtime/composables/useConvexConnectionState.d.ts +33 -0
  47. package/dist/runtime/composables/useConvexConnectionState.js +24 -0
  48. package/dist/runtime/composables/useConvexFileUpload.d.ts +169 -0
  49. package/dist/runtime/composables/useConvexFileUpload.js +198 -0
  50. package/dist/runtime/composables/useConvexMutation.d.ts +13 -0
  51. package/dist/runtime/composables/useConvexMutation.js +24 -0
  52. package/dist/runtime/composables/useConvexPaginatedQuery.d.ts +13 -0
  53. package/dist/runtime/composables/useConvexPaginatedQuery.js +256 -0
  54. package/dist/runtime/composables/useConvexQuery.d.ts +19 -0
  55. package/dist/runtime/composables/useConvexQuery.js +308 -0
  56. package/dist/runtime/convex-auth/adapter/create-adapter.d.ts +33 -0
  57. package/dist/runtime/convex-auth/adapter/create-adapter.js +308 -0
  58. package/dist/runtime/convex-auth/adapter/define-functions.d.ts +160 -0
  59. package/dist/runtime/convex-auth/adapter/define-functions.js +497 -0
  60. package/dist/runtime/convex-auth/adapter/generate-schema.d.ts +16 -0
  61. package/dist/runtime/convex-auth/adapter/generate-schema.js +332 -0
  62. package/dist/runtime/convex-auth/adapter/metadata.d.ts +44 -0
  63. package/dist/runtime/convex-auth/adapter/metadata.js +91 -0
  64. package/dist/runtime/convex-auth/adapter/query.d.ts +32 -0
  65. package/dist/runtime/convex-auth/adapter/query.js +275 -0
  66. package/dist/runtime/convex-auth/adapter/relationships.d.ts +20 -0
  67. package/dist/runtime/convex-auth/adapter/relationships.js +128 -0
  68. package/dist/runtime/convex-auth/component/_generated/api.js +5 -0
  69. package/dist/runtime/convex-auth/component/_generated/component.d.ts +116 -0
  70. package/dist/runtime/convex-auth/component/_generated/component.js +0 -0
  71. package/dist/runtime/convex-auth/component/_generated/dataModel.js +0 -0
  72. package/dist/runtime/convex-auth/component/_generated/server.js +16 -0
  73. package/dist/runtime/convex-auth/component/adapter.d.ts +142 -0
  74. package/dist/runtime/convex-auth/component/adapter.js +16 -0
  75. package/dist/runtime/convex-auth/component/convex.config.d.ts +2 -0
  76. package/dist/runtime/convex-auth/component/convex.config.js +3 -0
  77. package/dist/runtime/convex-auth/component/schema.d.ts +799 -0
  78. package/dist/runtime/convex-auth/component/schema.js +180 -0
  79. package/dist/runtime/convex-auth/component/schemaMetadata.d.ts +2094 -0
  80. package/dist/runtime/convex-auth/component/schemaMetadata.js +2161 -0
  81. package/dist/runtime/convex-auth/context.d.ts +5 -0
  82. package/dist/runtime/convex-auth/context.js +8 -0
  83. package/dist/runtime/convex-auth/create-auth-component.d.ts +41 -0
  84. package/dist/runtime/convex-auth/create-auth-component.js +157 -0
  85. package/dist/runtime/convex-auth/index.d.ts +11 -0
  86. package/dist/runtime/convex-auth/index.js +7 -0
  87. package/dist/runtime/convex-auth/internal-session.d.ts +2 -0
  88. package/dist/runtime/convex-auth/internal-session.js +1 -0
  89. package/dist/runtime/convex-auth/jwks-rotation.d.ts +58 -0
  90. package/dist/runtime/convex-auth/jwks-rotation.js +163 -0
  91. package/dist/runtime/convex-auth/oauth-live-access.d.ts +17 -0
  92. package/dist/runtime/convex-auth/oauth-live-access.js +78 -0
  93. package/dist/runtime/convex-auth/oauth-provider-compat.d.ts +10 -0
  94. package/dist/runtime/convex-auth/oauth-provider-compat.js +170 -0
  95. package/dist/runtime/convex-auth/oauth-resource.d.ts +39 -0
  96. package/dist/runtime/convex-auth/oauth-resource.js +118 -0
  97. package/dist/runtime/convex-auth/oauth-security.d.ts +82 -0
  98. package/dist/runtime/convex-auth/oauth-security.js +454 -0
  99. package/dist/runtime/convex-auth/origin.d.ts +4 -0
  100. package/dist/runtime/convex-auth/origin.js +9 -0
  101. package/dist/runtime/convex-auth/plugin.d.ts +18 -0
  102. package/dist/runtime/convex-auth/plugin.js +605 -0
  103. package/dist/runtime/convex-auth/provider.d.ts +2 -0
  104. package/dist/runtime/convex-auth/provider.js +11 -0
  105. package/dist/runtime/convex-auth/test.d.ts +410 -0
  106. package/dist/runtime/convex-auth/test.js +9 -0
  107. package/dist/runtime/convex-auth/types.d.ts +18 -0
  108. package/dist/runtime/convex-auth/types.js +0 -0
  109. package/dist/runtime/convex-auth/user-projection.d.ts +106 -0
  110. package/dist/runtime/convex-auth/user-projection.js +128 -0
  111. package/dist/runtime/devtools/auth-proxy-registry.d.ts +4 -0
  112. package/dist/runtime/devtools/auth-proxy-registry.js +47 -0
  113. package/dist/runtime/devtools/bridge-setup.d.ts +21 -0
  114. package/dist/runtime/devtools/bridge-setup.js +165 -0
  115. package/dist/runtime/devtools/path-utils.d.ts +2 -0
  116. package/dist/runtime/devtools/path-utils.js +12 -0
  117. package/dist/runtime/devtools/server.d.ts +2 -0
  118. package/dist/runtime/devtools/server.js +108 -0
  119. package/dist/runtime/devtools/setup-client.d.ts +16 -0
  120. package/dist/runtime/devtools/setup-client.js +43 -0
  121. package/dist/runtime/devtools/sink.d.ts +18 -0
  122. package/dist/runtime/devtools/sink.js +110 -0
  123. package/dist/runtime/devtools/transport.d.ts +16 -0
  124. package/dist/runtime/devtools/transport.js +149 -0
  125. package/dist/runtime/devtools/types.d.ts +167 -0
  126. package/dist/runtime/devtools/types.js +1 -0
  127. package/dist/runtime/devtools/ui/dist/200.html +1 -0
  128. package/dist/runtime/devtools/ui/dist/404.html +1 -0
  129. package/dist/runtime/devtools/ui/dist/_nuxt/C4MASuCh.js +1 -0
  130. package/dist/runtime/devtools/ui/dist/_nuxt/DJHkqtZq.js +3 -0
  131. package/dist/runtime/devtools/ui/dist/_nuxt/DWwrCltl.js +2 -0
  132. package/dist/runtime/devtools/ui/dist/_nuxt/DZ_UiYoH.js +1 -0
  133. package/dist/runtime/devtools/ui/dist/_nuxt/_m9ujb-i.js +1 -0
  134. package/dist/runtime/devtools/ui/dist/_nuxt/builds/latest.json +1 -0
  135. package/dist/runtime/devtools/ui/dist/_nuxt/builds/meta/better-convex-nuxt-devtools.json +1 -0
  136. package/dist/runtime/devtools/ui/dist/_nuxt/entry.DpFDKYZ5.css +1 -0
  137. package/dist/runtime/devtools/ui/dist/_nuxt/error-404.DYUJo5xt.css +1 -0
  138. package/dist/runtime/devtools/ui/dist/_nuxt/error-500.DQdoJJXg.css +1 -0
  139. package/dist/runtime/devtools/ui/dist/convex-logo.svg +5 -0
  140. package/dist/runtime/devtools/ui/dist/index.html +1 -0
  141. package/dist/runtime/errors/index.d.ts +7 -0
  142. package/dist/runtime/errors/index.js +5 -0
  143. package/dist/runtime/middleware/convex-auth.global.d.ts +2 -0
  144. package/dist/runtime/middleware/convex-auth.global.js +31 -0
  145. package/dist/runtime/plugin.auth.client.d.ts +3 -0
  146. package/dist/runtime/plugin.auth.client.js +217 -0
  147. package/dist/runtime/plugin.client.d.ts +3 -0
  148. package/dist/runtime/plugin.client.js +43 -0
  149. package/dist/runtime/plugin.server.d.ts +12 -0
  150. package/dist/runtime/plugin.server.js +98 -0
  151. package/dist/runtime/plugins/convex-call-error-payload.d.ts +14 -0
  152. package/dist/runtime/plugins/convex-call-error-payload.js +12 -0
  153. package/dist/runtime/runtime-context.d.ts +26 -0
  154. package/dist/runtime/runtime-context.js +48 -0
  155. package/dist/runtime/server/api/auth/[...].d.ts +10 -0
  156. package/dist/runtime/server/api/auth/[...].js +509 -0
  157. package/dist/runtime/server/api/auth/authorization-server-metadata.d.ts +2 -0
  158. package/dist/runtime/server/api/auth/authorization-server-metadata.js +6 -0
  159. package/dist/runtime/server/api/auth/body-size.d.ts +15 -0
  160. package/dist/runtime/server/api/auth/body-size.js +179 -0
  161. package/dist/runtime/server/api/auth/headers.d.ts +6 -0
  162. package/dist/runtime/server/api/auth/headers.js +98 -0
  163. package/dist/runtime/server/api/auth/jwks.d.ts +2 -0
  164. package/dist/runtime/server/api/auth/jwks.js +6 -0
  165. package/dist/runtime/server/api/auth/security.d.ts +14 -0
  166. package/dist/runtime/server/api/auth/security.js +85 -0
  167. package/dist/runtime/server/api/release-fingerprint.d.ts +2 -0
  168. package/dist/runtime/server/api/release-fingerprint.js +36 -0
  169. package/dist/runtime/server/index.d.ts +1 -0
  170. package/dist/runtime/server/index.js +3 -0
  171. package/dist/runtime/server/plugins/runtime-config.d.ts +7 -0
  172. package/dist/runtime/server/plugins/runtime-config.js +7 -0
  173. package/dist/runtime/server/utils/auth-snapshot.d.ts +26 -0
  174. package/dist/runtime/server/utils/auth-snapshot.js +165 -0
  175. package/dist/runtime/server/utils/http.d.ts +16 -0
  176. package/dist/runtime/server/utils/http.js +133 -0
  177. package/dist/runtime/server/utils/server-convex-caller.d.ts +31 -0
  178. package/dist/runtime/server/utils/server-convex-caller.js +151 -0
  179. package/dist/runtime/server/utils/server-convex-options.d.ts +83 -0
  180. package/dist/runtime/server/utils/server-convex-options.js +69 -0
  181. package/dist/runtime/server/utils/signed-client-ip.d.ts +6 -0
  182. package/dist/runtime/server/utils/signed-client-ip.js +29 -0
  183. package/dist/runtime/server/utils/ssr-auth-headers.d.ts +26 -0
  184. package/dist/runtime/server/utils/ssr-auth-headers.js +49 -0
  185. package/dist/runtime/server/utils/token-exchange.d.ts +27 -0
  186. package/dist/runtime/server/utils/token-exchange.js +96 -0
  187. package/dist/runtime/shared/auth-cookie.d.ts +4 -0
  188. package/dist/runtime/shared/auth-cookie.js +25 -0
  189. package/dist/runtime/shared/auth-origin.d.ts +15 -0
  190. package/dist/runtime/shared/auth-origin.js +42 -0
  191. package/dist/runtime/shared/bounded-stream.d.ts +2 -0
  192. package/dist/runtime/shared/bounded-stream.js +50 -0
  193. package/dist/runtime/shared/client-ip.d.ts +21 -0
  194. package/dist/runtime/shared/client-ip.js +123 -0
  195. package/dist/runtime/shared/release-fingerprint.d.ts +5 -0
  196. package/dist/runtime/shared/release-fingerprint.js +17 -0
  197. package/dist/runtime/types.d.ts +19 -0
  198. package/dist/runtime/utils/auth-config.d.ts +24 -0
  199. package/dist/runtime/utils/auth-config.js +45 -0
  200. package/dist/runtime/utils/auth-contract.d.ts +22 -0
  201. package/dist/runtime/utils/auth-contract.js +0 -0
  202. package/dist/runtime/utils/auth-errors.d.ts +5 -0
  203. package/dist/runtime/utils/auth-errors.js +22 -0
  204. package/dist/runtime/utils/auth-identity-state.d.ts +4 -0
  205. package/dist/runtime/utils/auth-identity-state.js +8 -0
  206. package/dist/runtime/utils/auth-pending-state.d.ts +13 -0
  207. package/dist/runtime/utils/auth-pending-state.js +4 -0
  208. package/dist/runtime/utils/auth-pending.d.ts +10 -0
  209. package/dist/runtime/utils/auth-pending.js +30 -0
  210. package/dist/runtime/utils/auth-route-protection.d.ts +16 -0
  211. package/dist/runtime/utils/auth-route-protection.js +58 -0
  212. package/dist/runtime/utils/auth-status.d.ts +47 -0
  213. package/dist/runtime/utils/auth-status.js +8 -0
  214. package/dist/runtime/utils/bounded-convex-fetch.d.ts +18 -0
  215. package/dist/runtime/utils/bounded-convex-fetch.js +152 -0
  216. package/dist/runtime/utils/callable-devtools.d.ts +17 -0
  217. package/dist/runtime/utils/callable-devtools.js +49 -0
  218. package/dist/runtime/utils/callable-observer.d.ts +2 -0
  219. package/dist/runtime/utils/callable-observer.js +1 -0
  220. package/dist/runtime/utils/composable-scope.d.ts +2 -0
  221. package/dist/runtime/utils/composable-scope.js +8 -0
  222. package/dist/runtime/utils/config-defaults.d.ts +8 -0
  223. package/dist/runtime/utils/config-defaults.js +8 -0
  224. package/dist/runtime/utils/convex-cache.d.ts +68 -0
  225. package/dist/runtime/utils/convex-cache.js +47 -0
  226. package/dist/runtime/utils/convex-config.d.ts +16 -0
  227. package/dist/runtime/utils/convex-config.js +50 -0
  228. package/dist/runtime/utils/convex-shared.d.ts +67 -0
  229. package/dist/runtime/utils/convex-shared.js +134 -0
  230. package/dist/runtime/utils/identity-changed-error.d.ts +4 -0
  231. package/dist/runtime/utils/identity-changed-error.js +12 -0
  232. package/dist/runtime/utils/identity-key.d.ts +19 -0
  233. package/dist/runtime/utils/identity-key.js +10 -0
  234. package/dist/runtime/utils/integrated-auth-client.d.ts +5 -0
  235. package/dist/runtime/utils/integrated-auth-client.js +0 -0
  236. package/dist/runtime/utils/logger.d.ts +42 -0
  237. package/dist/runtime/utils/logger.js +190 -0
  238. package/dist/runtime/utils/mime-type.d.ts +33 -0
  239. package/dist/runtime/utils/mime-type.js +10 -0
  240. package/dist/runtime/utils/query-execution-gate.d.ts +47 -0
  241. package/dist/runtime/utils/query-execution-gate.js +63 -0
  242. package/dist/runtime/utils/query-execution.d.ts +8 -0
  243. package/dist/runtime/utils/query-execution.js +19 -0
  244. package/dist/runtime/utils/query-foundation.d.ts +20 -0
  245. package/dist/runtime/utils/query-foundation.js +39 -0
  246. package/dist/runtime/utils/query-state.d.ts +12 -0
  247. package/dist/runtime/utils/query-state.js +12 -0
  248. package/dist/runtime/utils/reactive-args.d.ts +5 -0
  249. package/dist/runtime/utils/reactive-args.js +42 -0
  250. package/dist/runtime/utils/runtime-config-normalize.d.ts +22 -0
  251. package/dist/runtime/utils/runtime-config-normalize.js +28 -0
  252. package/dist/runtime/utils/runtime-config.d.ts +4 -0
  253. package/dist/runtime/utils/runtime-config.js +8 -0
  254. package/dist/runtime/utils/sanitize-diagnostic.d.ts +6 -0
  255. package/dist/runtime/utils/sanitize-diagnostic.js +81 -0
  256. package/dist/runtime/utils/shared-helpers.d.ts +46 -0
  257. package/dist/runtime/utils/shared-helpers.js +139 -0
  258. package/dist/runtime/utils/site-url.d.ts +4 -0
  259. package/dist/runtime/utils/site-url.js +15 -0
  260. package/dist/runtime/utils/ssr-pagination-state.d.ts +3 -0
  261. package/dist/runtime/utils/ssr-pagination-state.js +3 -0
  262. package/dist/runtime/utils/types.d.ts +29 -0
  263. package/dist/runtime/utils/types.js +0 -0
  264. package/dist/runtime/utils/upload-core.d.ts +16 -0
  265. package/dist/runtime/utils/upload-core.js +116 -0
  266. package/dist/types.d.mts +21 -0
  267. package/package.json +253 -0
  268. package/security/upstream-convex-better-auth.json +477 -0
@@ -0,0 +1,308 @@
1
+ import {
2
+ createAdapterFactory
3
+ } from "better-auth/adapters";
4
+ import { symmetricDecrypt, symmetricEncrypt } from "better-auth/crypto";
5
+ import { createFunctionHandle } from "convex/server";
6
+ import { isWritableAuthCtx, requireWritableAuthCtx } from "../context.js";
7
+ import { createAuthSchema } from "./generate-schema.js";
8
+ function toComponentWhere(where) {
9
+ return where?.map((condition) => ({
10
+ ...condition,
11
+ value: condition.value instanceof Date ? condition.value.getTime() : Array.isArray(condition.value) ? [...condition.value] : condition.value
12
+ }));
13
+ }
14
+ const encryptedEnvelopePrefix = "$ba$";
15
+ function parseVersionedSecrets(raw) {
16
+ if (!raw) return [];
17
+ return raw.split(",").map((entry) => {
18
+ const separator = entry.indexOf(":");
19
+ const versionText = separator < 0 ? "" : entry.slice(0, separator).trim();
20
+ const value = separator < 0 ? "" : entry.slice(separator + 1).trim();
21
+ const version = Number(versionText);
22
+ if (!/^(?:0|[1-9]\d*)$/u.test(versionText) || !Number.isSafeInteger(version) || value.length === 0) {
23
+ throw new Error("AUTH_VERSIONED_SECRETS_INVALID");
24
+ }
25
+ return { value, version };
26
+ });
27
+ }
28
+ function idTokenSecretConfig(options) {
29
+ const entries = options.secrets ?? parseVersionedSecrets(process.env.BETTER_AUTH_SECRETS);
30
+ if (entries.length === 0) throw new Error("AUTH_VERSIONED_SECRETS_REQUIRED");
31
+ const keys = /* @__PURE__ */ new Map();
32
+ for (const entry of entries) {
33
+ if (keys.has(entry.version)) throw new Error("AUTH_VERSIONED_SECRETS_INVALID");
34
+ keys.set(entry.version, entry.value);
35
+ }
36
+ return {
37
+ currentVersion: entries[0].version,
38
+ keys
39
+ };
40
+ }
41
+ function assertNoLegacyAuthSecretEnvironment() {
42
+ if (process.env.BETTER_AUTH_SECRET !== void 0 || process.env.AUTH_SECRET !== void 0) {
43
+ throw new Error("AUTH_LEGACY_SECRET_UNSUPPORTED");
44
+ }
45
+ }
46
+ export function createAccountIdTokenProtector(options) {
47
+ assertNoLegacyAuthSecretEnvironment();
48
+ const accountModel = options.account?.modelName ?? "account";
49
+ const idTokenField = options.account?.fields?.idToken ?? "idToken";
50
+ const transform = async (model, data, direction) => {
51
+ if (model !== accountModel || !data || typeof data !== "object" || Array.isArray(data) || !(idTokenField in data)) {
52
+ return data;
53
+ }
54
+ const value = data[idTokenField];
55
+ if (value === null || value === void 0 || value === "") return data;
56
+ if (typeof value !== "string") throw new Error("AUTH_ID_TOKEN_INVALID");
57
+ if (options.account?.encryptOAuthTokens !== true) {
58
+ throw new Error("AUTH_OAUTH_TOKEN_ENCRYPTION_REQUIRED");
59
+ }
60
+ const secretConfig = idTokenSecretConfig(options);
61
+ if (direction === "reveal" && !value.startsWith(encryptedEnvelopePrefix)) {
62
+ throw new Error("AUTH_ID_TOKEN_AT_REST_UNENCRYPTED");
63
+ }
64
+ let transformed;
65
+ try {
66
+ if (direction === "protect") {
67
+ if (value.startsWith(encryptedEnvelopePrefix)) {
68
+ await symmetricDecrypt({ data: value, key: secretConfig });
69
+ transformed = value;
70
+ } else {
71
+ transformed = await symmetricEncrypt({ data: value, key: secretConfig });
72
+ }
73
+ } else {
74
+ transformed = await symmetricDecrypt({ data: value, key: secretConfig });
75
+ }
76
+ } catch {
77
+ throw new Error(
78
+ direction === "protect" ? "AUTH_ID_TOKEN_ENCRYPTION_FAILED" : "AUTH_ID_TOKEN_DECRYPTION_FAILED"
79
+ );
80
+ }
81
+ return { ...data, [idTokenField]: transformed };
82
+ };
83
+ return {
84
+ protect: (model, data) => transform(model, data, "protect"),
85
+ reveal: (model, data) => transform(model, data, "reveal")
86
+ };
87
+ }
88
+ function toEpochMilliseconds(data) {
89
+ if (data === null || data === void 0) return data;
90
+ const value = data instanceof Date ? data.getTime() : new Date(data).getTime();
91
+ if (!Number.isFinite(value)) throw new Error("AUTH_DATE_INVALID");
92
+ return value;
93
+ }
94
+ function toDate(data) {
95
+ if (data === null || data === void 0 || data instanceof Date) return data;
96
+ if (typeof data !== "number" || !Number.isFinite(data)) throw new Error("AUTH_DATE_INVALID");
97
+ return new Date(data);
98
+ }
99
+ async function collectPages(next, limit) {
100
+ if (limit !== void 0 && (!Number.isSafeInteger(limit) || limit < 0)) {
101
+ throw new Error("AUTH_LIMIT_INVALID");
102
+ }
103
+ if (limit === 0) return [];
104
+ const rows = [];
105
+ let cursor = null;
106
+ let complete = false;
107
+ while (!complete) {
108
+ const remaining = limit === void 0 ? 200 : Math.min(200, limit - rows.length);
109
+ if (remaining <= 0) break;
110
+ const cursorBefore = cursor;
111
+ const result = await next({ cursor, numItems: remaining });
112
+ rows.push(...result.page);
113
+ if (result.isDone || limit !== void 0 && rows.length >= limit) {
114
+ complete = true;
115
+ continue;
116
+ }
117
+ cursor = result.continueCursor;
118
+ if (cursor === cursorBefore && result.page.length === 0) {
119
+ throw new Error("AUTH_PAGINATION_STALLED");
120
+ }
121
+ }
122
+ return limit === void 0 ? rows : rows.slice(0, limit);
123
+ }
124
+ function configuredTriggerModels(triggers, event) {
125
+ return Object.entries(triggers ?? {}).flatMap(
126
+ ([model, configured]) => typeof configured?.[event] === "function" ? [model] : []
127
+ );
128
+ }
129
+ export function createConvexAuthAdapter(ctx, component, adapterOptions = {}) {
130
+ const handleCache = /* @__PURE__ */ new Map();
131
+ return createAdapterFactory({
132
+ config: {
133
+ adapterId: "@lupinum/better-convex-nuxt",
134
+ adapterName: "Better Convex Nuxt",
135
+ debugLogs: adapterOptions.debugLogs ?? false,
136
+ transaction: false,
137
+ supportsNumericIds: false,
138
+ supportsUUIDs: false,
139
+ supportsJSON: false,
140
+ supportsDates: false,
141
+ supportsBooleans: true,
142
+ supportsArrays: true,
143
+ usePlural: false,
144
+ customTransformInput: ({ data, fieldAttributes }) => fieldAttributes.type === "date" ? toEpochMilliseconds(data) : data,
145
+ customTransformOutput: ({ data, fieldAttributes }) => fieldAttributes.type === "date" ? toDate(data) : data
146
+ },
147
+ adapter: ({ getFieldName, getModelName, options }) => {
148
+ options.telemetry = { enabled: false };
149
+ if (options.advanced?.database?.joins) throw new Error("AUTH_JOINS_UNSUPPORTED");
150
+ const idTokens = createAccountIdTokenProtector(options);
151
+ const triggerModels = {
152
+ onCreate: configuredTriggerModels(adapterOptions.triggers, "onCreate").map(getModelName),
153
+ onDelete: configuredTriggerModels(adapterOptions.triggers, "onDelete").map(getModelName),
154
+ onUpdate: configuredTriggerModels(adapterOptions.triggers, "onUpdate").map(getModelName)
155
+ };
156
+ const triggerHandle = (model, event) => {
157
+ const cacheKey = `${event}:${model}`;
158
+ const existing = handleCache.get(cacheKey);
159
+ if (existing) return existing;
160
+ const reference = adapterOptions.authFunctions?.[event];
161
+ const value = reference && triggerModels[event].includes(model) ? createFunctionHandle(reference).then(String) : Promise.resolve(void 0);
162
+ handleCache.set(cacheKey, value);
163
+ return value;
164
+ };
165
+ const relationshipTriggerHandle = (event) => {
166
+ const cacheKey = `relationship:${event}`;
167
+ const existing = handleCache.get(cacheKey);
168
+ if (existing) return existing;
169
+ const reference = adapterOptions.authFunctions?.[event];
170
+ const value = reference && triggerModels[event].length > 0 ? createFunctionHandle(reference).then(String) : Promise.resolve(void 0);
171
+ handleCache.set(cacheKey, value);
172
+ return value;
173
+ };
174
+ const mapSelect = (model, select) => select?.map((field) => getFieldName({ model, field }));
175
+ const mapSort = (model, sortBy) => sortBy ? { ...sortBy, field: getFieldName({ model, field: sortBy.field }) } : void 0;
176
+ const adapter = {
177
+ options: { isRunMutationCtx: isWritableAuthCtx(ctx) },
178
+ createSchema: createAuthSchema,
179
+ create: async ({
180
+ model,
181
+ data
182
+ }) => {
183
+ requireWritableAuthCtx(ctx);
184
+ const created = await ctx.runMutation(component.adapter.create, {
185
+ model,
186
+ data: await idTokens.protect(model, data),
187
+ onCreateHandle: await triggerHandle(model, "onCreate")
188
+ });
189
+ return idTokens.reveal(model, created);
190
+ },
191
+ findOne: async ({
192
+ model,
193
+ where,
194
+ select
195
+ }) => {
196
+ const found = await ctx.runQuery(component.adapter.findOne, {
197
+ model,
198
+ where: toComponentWhere(where),
199
+ select: mapSelect(model, select)
200
+ });
201
+ return idTokens.reveal(model, found);
202
+ },
203
+ findMany: async ({
204
+ model,
205
+ where,
206
+ limit,
207
+ select,
208
+ sortBy,
209
+ offset
210
+ }) => {
211
+ if (offset !== void 0 && offset !== 0) throw new Error("AUTH_OFFSET_UNSUPPORTED");
212
+ const componentSelect = mapSelect(model, select);
213
+ const componentSortBy = mapSort(model, sortBy);
214
+ const rows = await collectPages(
215
+ (paginationOpts) => ctx.runQuery(component.adapter.findMany, {
216
+ model,
217
+ where: toComponentWhere(where),
218
+ select: componentSelect,
219
+ sortBy: componentSortBy,
220
+ paginationOpts
221
+ }),
222
+ limit
223
+ );
224
+ return Promise.all(rows.map((row) => idTokens.reveal(model, row)));
225
+ },
226
+ count: ({ model, where }) => ctx.runQuery(component.adapter.count, { model, where: toComponentWhere(where) }),
227
+ update: async ({
228
+ model,
229
+ where,
230
+ update
231
+ }) => {
232
+ requireWritableAuthCtx(ctx);
233
+ if (where.length === 0) return null;
234
+ const updated = await ctx.runMutation(component.adapter.updateOne, {
235
+ model,
236
+ where: toComponentWhere(where),
237
+ update: await idTokens.protect(model, update),
238
+ onUpdateHandle: await triggerHandle(model, "onUpdate")
239
+ });
240
+ return idTokens.reveal(model, updated);
241
+ },
242
+ updateMany: async ({ model, where, update }) => {
243
+ requireWritableAuthCtx(ctx);
244
+ return ctx.runMutation(component.adapter.updateMany, {
245
+ model,
246
+ where: toComponentWhere(where),
247
+ update: await idTokens.protect(model, update),
248
+ onUpdateHandle: await triggerHandle(model, "onUpdate")
249
+ });
250
+ },
251
+ delete: async ({ model, where }) => {
252
+ requireWritableAuthCtx(ctx);
253
+ await ctx.runMutation(component.adapter.deleteOne, {
254
+ model,
255
+ where: toComponentWhere(where),
256
+ onDeleteHandle: await relationshipTriggerHandle("onDelete"),
257
+ onDeleteModels: triggerModels.onDelete,
258
+ onUpdateHandle: await relationshipTriggerHandle("onUpdate"),
259
+ onUpdateModels: triggerModels.onUpdate
260
+ });
261
+ },
262
+ deleteMany: async ({ model, where }) => {
263
+ requireWritableAuthCtx(ctx);
264
+ return ctx.runMutation(component.adapter.deleteMany, {
265
+ model,
266
+ where: toComponentWhere(where),
267
+ onDeleteHandle: await relationshipTriggerHandle("onDelete"),
268
+ onDeleteModels: triggerModels.onDelete,
269
+ onUpdateHandle: await relationshipTriggerHandle("onUpdate"),
270
+ onUpdateModels: triggerModels.onUpdate
271
+ });
272
+ },
273
+ consumeOne: async ({
274
+ model,
275
+ where
276
+ }) => {
277
+ requireWritableAuthCtx(ctx);
278
+ const consumed = await ctx.runMutation(component.adapter.consumeOne, {
279
+ model,
280
+ where: toComponentWhere(where),
281
+ onDeleteHandle: await relationshipTriggerHandle("onDelete"),
282
+ onDeleteModels: triggerModels.onDelete,
283
+ onUpdateHandle: await relationshipTriggerHandle("onUpdate"),
284
+ onUpdateModels: triggerModels.onUpdate
285
+ });
286
+ return idTokens.reveal(model, consumed);
287
+ },
288
+ incrementOne: async ({
289
+ model,
290
+ where,
291
+ increment,
292
+ set
293
+ }) => {
294
+ requireWritableAuthCtx(ctx);
295
+ const incremented = await ctx.runMutation(component.adapter.incrementOne, {
296
+ model,
297
+ where: toComponentWhere(where),
298
+ increment,
299
+ set: await idTokens.protect(model, set),
300
+ onUpdateHandle: await triggerHandle(model, "onUpdate")
301
+ });
302
+ return idTokens.reveal(model, incremented);
303
+ }
304
+ };
305
+ return adapter;
306
+ }
307
+ });
308
+ }
@@ -0,0 +1,160 @@
1
+ import { type SchemaDefinition } from 'convex/server';
2
+ import type { AuthSchemaMetadata } from './metadata.js';
3
+ export interface DefineAuthAdapterFunctionsOptions<Schema extends SchemaDefinition<any, any>> {
4
+ schema: Schema;
5
+ metadata: AuthSchemaMetadata;
6
+ }
7
+ export declare function defineAuthAdapterFunctions<Schema extends SchemaDefinition<any, any>>({ schema, metadata, }: DefineAuthAdapterFunctionsOptions<Schema>): {
8
+ create: import("convex/server").RegisteredMutation<"public", {
9
+ onCreateHandle?: string | undefined;
10
+ data: any;
11
+ model: string;
12
+ }, Promise<Record<string, unknown> | null>>;
13
+ findOne: import("convex/server").RegisteredQuery<"public", {
14
+ join?: any;
15
+ select?: string[] | undefined;
16
+ where?: {
17
+ mode?: "sensitive" | "insensitive" | undefined;
18
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
19
+ connector?: "AND" | "OR" | undefined;
20
+ value: string | number | boolean | string[] | number[] | null;
21
+ field: string;
22
+ }[] | undefined;
23
+ offset?: number | undefined;
24
+ sortBy?: {
25
+ field: string;
26
+ direction: "asc" | "desc";
27
+ } | undefined;
28
+ model: string;
29
+ }, Promise<Record<string, unknown> | null>>;
30
+ findMany: import("convex/server").RegisteredQuery<"public", {
31
+ join?: any;
32
+ select?: string[] | undefined;
33
+ where?: {
34
+ mode?: "sensitive" | "insensitive" | undefined;
35
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
36
+ connector?: "AND" | "OR" | undefined;
37
+ value: string | number | boolean | string[] | number[] | null;
38
+ field: string;
39
+ }[] | undefined;
40
+ limit?: number | undefined;
41
+ offset?: number | undefined;
42
+ sortBy?: {
43
+ field: string;
44
+ direction: "asc" | "desc";
45
+ } | undefined;
46
+ paginationOpts: {
47
+ id?: number;
48
+ endCursor?: string | null;
49
+ maximumRowsRead?: number;
50
+ maximumBytesRead?: number;
51
+ numItems: number;
52
+ cursor: string | null;
53
+ };
54
+ model: string;
55
+ }, Promise<any>>;
56
+ count: import("convex/server").RegisteredQuery<"public", {
57
+ where?: {
58
+ mode?: "sensitive" | "insensitive" | undefined;
59
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
60
+ connector?: "AND" | "OR" | undefined;
61
+ value: string | number | boolean | string[] | number[] | null;
62
+ field: string;
63
+ }[] | undefined;
64
+ model: string;
65
+ }, Promise<number>>;
66
+ updateOne: import("convex/server").RegisteredMutation<"public", {
67
+ onUpdateHandle?: string | undefined;
68
+ update: any;
69
+ model: string;
70
+ where: {
71
+ mode?: "sensitive" | "insensitive" | undefined;
72
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
73
+ connector?: "AND" | "OR" | undefined;
74
+ value: string | number | boolean | string[] | number[] | null;
75
+ field: string;
76
+ }[];
77
+ }, Promise<Record<string, unknown> | null>>;
78
+ updateMany: import("convex/server").RegisteredMutation<"public", {
79
+ onUpdateHandle?: string | undefined;
80
+ update: any;
81
+ model: string;
82
+ where: {
83
+ mode?: "sensitive" | "insensitive" | undefined;
84
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
85
+ connector?: "AND" | "OR" | undefined;
86
+ value: string | number | boolean | string[] | number[] | null;
87
+ field: string;
88
+ }[];
89
+ }, Promise<number>>;
90
+ deleteOne: import("convex/server").RegisteredMutation<"public", {
91
+ onDeleteHandle?: string | undefined;
92
+ onDeleteModels?: string[] | undefined;
93
+ onUpdateHandle?: string | undefined;
94
+ onUpdateModels?: string[] | undefined;
95
+ model: string;
96
+ where: {
97
+ mode?: "sensitive" | "insensitive" | undefined;
98
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
99
+ connector?: "AND" | "OR" | undefined;
100
+ value: string | number | boolean | string[] | number[] | null;
101
+ field: string;
102
+ }[];
103
+ }, Promise<Record<string, unknown> | null>>;
104
+ deleteMany: import("convex/server").RegisteredMutation<"public", {
105
+ onDeleteHandle?: string | undefined;
106
+ onDeleteModels?: string[] | undefined;
107
+ onUpdateHandle?: string | undefined;
108
+ onUpdateModels?: string[] | undefined;
109
+ model: string;
110
+ where: {
111
+ mode?: "sensitive" | "insensitive" | undefined;
112
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
113
+ connector?: "AND" | "OR" | undefined;
114
+ value: string | number | boolean | string[] | number[] | null;
115
+ field: string;
116
+ }[];
117
+ }, Promise<number>>;
118
+ consumeOne: import("convex/server").RegisteredMutation<"public", {
119
+ onDeleteHandle?: string | undefined;
120
+ onDeleteModels?: string[] | undefined;
121
+ onUpdateHandle?: string | undefined;
122
+ onUpdateModels?: string[] | undefined;
123
+ model: string;
124
+ where: {
125
+ mode?: "sensitive" | "insensitive" | undefined;
126
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
127
+ connector?: "AND" | "OR" | undefined;
128
+ value: string | number | boolean | string[] | number[] | null;
129
+ field: string;
130
+ }[];
131
+ }, Promise<Record<string, unknown> | null>>;
132
+ incrementOne: import("convex/server").RegisteredMutation<"public", {
133
+ set?: any;
134
+ onUpdateHandle?: string | undefined;
135
+ model: string;
136
+ where: {
137
+ mode?: "sensitive" | "insensitive" | undefined;
138
+ operator?: "in" | "lt" | "lte" | "gt" | "gte" | "eq" | "not_in" | "ne" | "contains" | "starts_with" | "ends_with" | undefined;
139
+ connector?: "AND" | "OR" | undefined;
140
+ value: string | number | boolean | string[] | number[] | null;
141
+ field: string;
142
+ }[];
143
+ increment: any;
144
+ }, Promise<Record<string, unknown> | null>>;
145
+ rotateSigningKey: import("convex/server").RegisteredMutation<"public", {
146
+ next: {
147
+ id: string;
148
+ alg: "RS256";
149
+ crv: null;
150
+ privateKey: string;
151
+ publicKey: string;
152
+ };
153
+ }, Promise<{
154
+ createdAt: number;
155
+ newKid: string;
156
+ previousKids: string[];
157
+ previousVerifyUntil: number;
158
+ rotatedAt: number;
159
+ }>>;
160
+ };