@convex-dev/better-auth 0.7.0-alpha.8 → 0.7.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 (107) hide show
  1. package/dist/commonjs/client/adapter.d.ts +10 -1
  2. package/dist/commonjs/client/adapter.d.ts.map +1 -1
  3. package/dist/commonjs/client/adapter.js +190 -192
  4. package/dist/commonjs/client/adapter.js.map +1 -1
  5. package/dist/commonjs/client/index.d.ts +283 -179
  6. package/dist/commonjs/client/index.d.ts.map +1 -1
  7. package/dist/commonjs/client/index.js +59 -67
  8. package/dist/commonjs/client/index.js.map +1 -1
  9. package/dist/commonjs/component/adapterTest.d.ts +19 -0
  10. package/dist/commonjs/component/adapterTest.d.ts.map +1 -0
  11. package/dist/commonjs/component/adapterTest.js +82 -0
  12. package/dist/commonjs/component/adapterTest.js.map +1 -0
  13. package/dist/commonjs/component/lib.d.ts +308 -536
  14. package/dist/commonjs/component/lib.d.ts.map +1 -1
  15. package/dist/commonjs/component/lib.js +469 -292
  16. package/dist/commonjs/component/lib.js.map +1 -1
  17. package/dist/commonjs/component/schema.d.ts +465 -26
  18. package/dist/commonjs/component/schema.d.ts.map +1 -1
  19. package/dist/commonjs/component/schema.js +334 -18
  20. package/dist/commonjs/component/schema.js.map +1 -1
  21. package/dist/commonjs/component/util.d.ts +944 -68
  22. package/dist/commonjs/component/util.d.ts.map +1 -1
  23. package/dist/commonjs/nextjs/index.d.ts.map +1 -1
  24. package/dist/commonjs/nextjs/index.js +3 -9
  25. package/dist/commonjs/nextjs/index.js.map +1 -1
  26. package/dist/commonjs/plugins/convex/index.d.ts +14 -11
  27. package/dist/commonjs/plugins/convex/index.d.ts.map +1 -1
  28. package/dist/commonjs/plugins/convex/index.js +3 -2
  29. package/dist/commonjs/plugins/convex/index.js.map +1 -1
  30. package/dist/commonjs/plugins/cross-domain/client.d.ts +1 -1
  31. package/dist/commonjs/plugins/cross-domain/index.d.ts +5 -3
  32. package/dist/commonjs/plugins/cross-domain/index.d.ts.map +1 -1
  33. package/dist/commonjs/plugins/cross-domain/index.js +19 -5
  34. package/dist/commonjs/plugins/cross-domain/index.js.map +1 -1
  35. package/dist/commonjs/react/client.d.ts +1 -1
  36. package/dist/commonjs/react/client.d.ts.map +1 -1
  37. package/dist/commonjs/react/client.js +3 -9
  38. package/dist/commonjs/react/client.js.map +1 -1
  39. package/dist/commonjs/react-start/index.d.ts +4 -4
  40. package/dist/commonjs/react-start/index.d.ts.map +1 -1
  41. package/dist/commonjs/react-start/index.js +3 -0
  42. package/dist/commonjs/react-start/index.js.map +1 -1
  43. package/dist/commonjs/utils/index.d.ts +2 -0
  44. package/dist/commonjs/utils/index.d.ts.map +1 -0
  45. package/dist/commonjs/utils/index.js +8 -0
  46. package/dist/commonjs/utils/index.js.map +1 -0
  47. package/dist/esm/client/adapter.d.ts +10 -1
  48. package/dist/esm/client/adapter.d.ts.map +1 -1
  49. package/dist/esm/client/adapter.js +190 -192
  50. package/dist/esm/client/adapter.js.map +1 -1
  51. package/dist/esm/client/index.d.ts +283 -179
  52. package/dist/esm/client/index.d.ts.map +1 -1
  53. package/dist/esm/client/index.js +59 -67
  54. package/dist/esm/client/index.js.map +1 -1
  55. package/dist/esm/component/adapterTest.d.ts +19 -0
  56. package/dist/esm/component/adapterTest.d.ts.map +1 -0
  57. package/dist/esm/component/adapterTest.js +82 -0
  58. package/dist/esm/component/adapterTest.js.map +1 -0
  59. package/dist/esm/component/lib.d.ts +308 -536
  60. package/dist/esm/component/lib.d.ts.map +1 -1
  61. package/dist/esm/component/lib.js +469 -292
  62. package/dist/esm/component/lib.js.map +1 -1
  63. package/dist/esm/component/schema.d.ts +465 -26
  64. package/dist/esm/component/schema.d.ts.map +1 -1
  65. package/dist/esm/component/schema.js +334 -18
  66. package/dist/esm/component/schema.js.map +1 -1
  67. package/dist/esm/component/util.d.ts +944 -68
  68. package/dist/esm/component/util.d.ts.map +1 -1
  69. package/dist/esm/nextjs/index.d.ts.map +1 -1
  70. package/dist/esm/nextjs/index.js +3 -9
  71. package/dist/esm/nextjs/index.js.map +1 -1
  72. package/dist/esm/plugins/convex/index.d.ts +14 -11
  73. package/dist/esm/plugins/convex/index.d.ts.map +1 -1
  74. package/dist/esm/plugins/convex/index.js +3 -2
  75. package/dist/esm/plugins/convex/index.js.map +1 -1
  76. package/dist/esm/plugins/cross-domain/client.d.ts +1 -1
  77. package/dist/esm/plugins/cross-domain/index.d.ts +5 -3
  78. package/dist/esm/plugins/cross-domain/index.d.ts.map +1 -1
  79. package/dist/esm/plugins/cross-domain/index.js +19 -5
  80. package/dist/esm/plugins/cross-domain/index.js.map +1 -1
  81. package/dist/esm/react/client.d.ts +1 -1
  82. package/dist/esm/react/client.d.ts.map +1 -1
  83. package/dist/esm/react/client.js +3 -9
  84. package/dist/esm/react/client.js.map +1 -1
  85. package/dist/esm/react-start/index.d.ts +4 -4
  86. package/dist/esm/react-start/index.d.ts.map +1 -1
  87. package/dist/esm/react-start/index.js +3 -0
  88. package/dist/esm/react-start/index.js.map +1 -1
  89. package/dist/esm/utils/index.d.ts +2 -0
  90. package/dist/esm/utils/index.d.ts.map +1 -0
  91. package/dist/esm/utils/index.js +8 -0
  92. package/dist/esm/utils/index.js.map +1 -0
  93. package/package.json +24 -7
  94. package/src/client/adapter.test.ts +378 -0
  95. package/src/client/adapter.ts +206 -198
  96. package/src/client/index.ts +60 -80
  97. package/src/component/_generated/api.d.ts +2189 -171
  98. package/src/component/adapterTest.ts +141 -0
  99. package/src/component/lib.ts +648 -342
  100. package/src/component/schema.ts +349 -18
  101. package/src/nextjs/index.ts +3 -14
  102. package/src/plugins/convex/index.ts +5 -2
  103. package/src/plugins/cross-domain/index.ts +19 -5
  104. package/src/react/client.tsx +5 -11
  105. package/src/react-start/index.ts +4 -1
  106. package/src/client/cors.ts +0 -425
  107. /package/src/{util.ts → utils/index.ts} +0 -0
@@ -1,425 +0,0 @@
1
- /**
2
- * This file defines a CorsHttpRouter class that extends Convex's HttpRouter.
3
- * It provides CORS (Cross-Origin Resource Sharing) support for HTTP routes.
4
- *
5
- * The CorsHttpRouter:
6
- * 1. Allows specifying allowed origins for CORS.
7
- * 2. Overrides the route method to add CORS headers to all non-OPTIONS requests.
8
- * 3. Automatically adds an OPTIONS route to handle CORS preflight requests.
9
- * 4. Uses the handleCors helper function to apply CORS headers consistently.
10
- *
11
- * This router simplifies the process of making Convex HTTP endpoints
12
- * accessible to web applications hosted on different domains while
13
- * maintaining proper CORS configuration.
14
- */
15
- import {
16
- type GenericActionCtx,
17
- httpActionGeneric,
18
- httpRouter,
19
- HttpRouter,
20
- ROUTABLE_HTTP_METHODS,
21
- type RoutableMethod,
22
- type PublicHttpAction,
23
- type RouteSpec,
24
- type RouteSpecWithPath,
25
- type RouteSpecWithPathPrefix,
26
- } from "convex/server";
27
-
28
- export const DEFAULT_EXPOSED_HEADERS = [
29
- // For Range requests
30
- "Content-Range",
31
- "Accept-Ranges",
32
- ];
33
-
34
- export type CorsConfig = {
35
- /**
36
- * Whether to allow credentials in the request.
37
- * When true, the request can include cookies and authentication headers.
38
- * @default false
39
- */
40
- allowCredentials?: boolean;
41
- /**
42
- * An array of allowed origins: what domains are allowed to make requests.
43
- * For example, ["https://example.com"] would only allow requests from
44
- * https://example.com.
45
- * You can also use wildcards to allow all subdomains of a given domain.
46
- * E.g. ["*.example.com"] would allow requests from:
47
- * - https://subdomain.example.com
48
- * - https://example.com
49
- * @default ["*"]
50
- */
51
- allowedOrigins?: string[] | ((req: Request) => Promise<string[]>);
52
- /**
53
- * An array of allowed headers: what headers are allowed to be sent in
54
- * the request.
55
- * @default ["Content-Type"]
56
- */
57
- allowedHeaders?: string[];
58
- /**
59
- * An array of exposed headers: what headers are allowed to be sent in
60
- * the response.
61
- * Note: if you pass in an empty array, it will not expose any headers.
62
- * If you want to extend the default exposed headers, you can do so by
63
- * passing in [...DEFAULT_EXPOSED_HEADERS, ...yourHeaders].
64
- * @default {@link DEFAULT_EXPOSED_HEADERS}
65
- */
66
- exposedHeaders?: string[];
67
- /**
68
- * The maximum age of the preflight request in seconds.
69
- * @default 86400 (1 day)
70
- */
71
- browserCacheMaxAge?: number;
72
- /**
73
- * Whether to block requests from origins that are not in the allowedOrigins list.
74
- * @default true
75
- */
76
- enforceAllowOrigins?: boolean;
77
- /**
78
- * Whether to log debugging information about CORS requests.
79
- * @default false
80
- */
81
- debug?: boolean;
82
- };
83
-
84
- type RouteSpecWithCors = RouteSpec & CorsConfig;
85
-
86
- /**
87
- * Factory function to create a router that adds CORS support to routes.
88
- * @param allowedOrigins An array of allowed origins for CORS.
89
- * @returns A function to use instead of http.route when you want CORS.
90
- */
91
- export const corsRouter = (http: HttpRouter, corsConfig?: CorsConfig) => {
92
- const allowedExactMethodsByPath: Map<string, Set<string>> = new Map();
93
- const allowedPrefixMethodsByPath: Map<string, Set<string>> = new Map();
94
- return {
95
- http,
96
- route: (routeSpec: RouteSpecWithCors): void => {
97
- const tempRouter = httpRouter();
98
- tempRouter.exactRoutes = http.exactRoutes;
99
- tempRouter.prefixRoutes = http.prefixRoutes;
100
-
101
- const config = {
102
- ...corsConfig,
103
- ...routeSpec,
104
- };
105
-
106
- const httpCorsHandler = handleCors({
107
- originalHandler: routeSpec.handler,
108
- allowedMethods: [routeSpec.method],
109
- ...config,
110
- });
111
- /**
112
- * Figure out what kind of route we're adding: exact or prefix and handle
113
- * accordingly.
114
- */
115
- if ("path" in routeSpec) {
116
- let methods = allowedExactMethodsByPath.get(routeSpec.path);
117
- if (!methods) {
118
- methods = new Set<string>();
119
- allowedExactMethodsByPath.set(routeSpec.path, methods);
120
- }
121
- methods.add(routeSpec.method);
122
- tempRouter.route({
123
- path: routeSpec.path,
124
- method: routeSpec.method,
125
- handler: httpCorsHandler,
126
- });
127
- handleExactRoute(tempRouter, routeSpec, config, Array.from(methods));
128
- } else {
129
- let methods = allowedPrefixMethodsByPath.get(routeSpec.pathPrefix);
130
- if (!methods) {
131
- methods = new Set<string>();
132
- allowedPrefixMethodsByPath.set(routeSpec.pathPrefix, methods);
133
- }
134
- methods.add(routeSpec.method);
135
- tempRouter.route({
136
- pathPrefix: routeSpec.pathPrefix,
137
- method: routeSpec.method,
138
- handler: httpCorsHandler,
139
- });
140
- handlePrefixRoute(tempRouter, routeSpec, config, Array.from(methods));
141
- }
142
-
143
- /**
144
- * Copy the routes from the temporary router to the main router.
145
- */
146
- http.exactRoutes = new Map(tempRouter.exactRoutes);
147
- http.prefixRoutes = new Map(tempRouter.prefixRoutes);
148
- },
149
- };
150
- };
151
-
152
- /**
153
- * Handles exact route matching and adds OPTIONS handler.
154
- * @param tempRouter Temporary router instance.
155
- * @param routeSpec Route specification for exact matching.
156
- */
157
- function handleExactRoute(
158
- tempRouter: HttpRouter,
159
- routeSpec: RouteSpecWithPath,
160
- config: CorsConfig,
161
- allowedMethods: string[]
162
- ): void {
163
- const currentMethodsForPath = tempRouter.exactRoutes.get(routeSpec.path);
164
- /**
165
- * Add the OPTIONS handler for the given path
166
- */
167
- const optionsHandler = createOptionsHandlerForMethods(allowedMethods, config);
168
- currentMethodsForPath?.set("OPTIONS", optionsHandler);
169
- tempRouter.exactRoutes.set(routeSpec.path, new Map(currentMethodsForPath));
170
- }
171
-
172
- /**
173
- * Handles prefix route matching and adds OPTIONS handler.
174
- * @param tempRouter Temporary router instance.
175
- * @param routeSpec Route specification for prefix matching.
176
- */
177
- function handlePrefixRoute(
178
- tempRouter: HttpRouter,
179
- routeSpec: RouteSpecWithPathPrefix,
180
- config: CorsConfig,
181
- allowedMethods: string[]
182
- ): void {
183
- /**
184
- * prefixRoutes is structured differently than exactRoutes. It's defined as
185
- * a Map<string, Map<string, PublicHttpAction>> where the KEY is the
186
- * METHOD and the VALUE is a map of paths and handlers.
187
- */
188
- const optionsHandler = createOptionsHandlerForMethods(allowedMethods, config);
189
-
190
- const optionsPrefixes =
191
- tempRouter.prefixRoutes.get("OPTIONS") ||
192
- new Map<string, PublicHttpAction>();
193
- optionsPrefixes.set(routeSpec.pathPrefix, optionsHandler);
194
-
195
- tempRouter.prefixRoutes.set("OPTIONS", optionsPrefixes);
196
- }
197
-
198
- /**
199
- * Creates an OPTIONS handler for the given HTTP methods.
200
- * @param methods Array of HTTP methods to be allowed.
201
- * @returns A CORS-enabled OPTIONS handler.
202
- */
203
- function createOptionsHandlerForMethods(
204
- methods: string[],
205
- config: CorsConfig
206
- ): PublicHttpAction {
207
- return handleCors({
208
- ...config,
209
- allowedMethods: methods,
210
- });
211
- }
212
-
213
- export default corsRouter;
214
-
215
- /**
216
- * handleCors() is a higher-order function that wraps a Convex HTTP action handler to add CORS support.
217
- * It allows for customization of allowed HTTP methods and origins for cross-origin requests.
218
- *
219
- * The function:
220
- * 1. Validates and normalizes the allowed HTTP methods.
221
- * 2. Generates appropriate CORS headers based on the provided configuration.
222
- * 3. Handles preflight OPTIONS requests automatically.
223
- * 4. Wraps the original handler to add CORS headers to its response.
224
- *
225
- * This helper simplifies the process of making Convex HTTP actions accessible
226
- * to web applications hosted on different domains.
227
- */
228
-
229
- const SECONDS_IN_A_DAY = 60 * 60 * 24;
230
-
231
- /**
232
- * Example CORS origins:
233
- * - "*" (allow all origins)
234
- * - "https://example.com" (allow a specific domain)
235
- * - "https://*.example.com" (allow all subdomains of example.com)
236
- * - "https://example1.com, https://example2.com" (allow multiple specific domains)
237
- * - "null" (allow requests from data URLs or local files)
238
- */
239
-
240
- const handleCors = ({
241
- originalHandler,
242
- allowedMethods = ["OPTIONS"],
243
- allowedOrigins = ["*"],
244
- allowedHeaders = ["Content-Type"],
245
- exposedHeaders = DEFAULT_EXPOSED_HEADERS,
246
- allowCredentials = false,
247
- browserCacheMaxAge = SECONDS_IN_A_DAY,
248
- enforceAllowOrigins = true,
249
- debug = false,
250
- }: {
251
- originalHandler?: PublicHttpAction;
252
- allowedMethods?: string[];
253
- } & CorsConfig) => {
254
- const uniqueMethods = Array.from(
255
- new Set(
256
- allowedMethods.map((method) => method.toUpperCase() as RoutableMethod)
257
- )
258
- );
259
- const filteredMethods = uniqueMethods.filter((method) =>
260
- ROUTABLE_HTTP_METHODS.includes(method)
261
- );
262
-
263
- if (filteredMethods.length === 0) {
264
- throw new Error("No valid HTTP methods provided");
265
- }
266
-
267
- /**
268
- * Ensure OPTIONS is not duplicated if it was passed in
269
- * E.g. if allowedMethods = ["GET", "OPTIONS"]
270
- */
271
- const allowMethods = filteredMethods.includes("OPTIONS")
272
- ? filteredMethods.join(", ")
273
- : [...filteredMethods].join(", ");
274
-
275
- /**
276
- * Build up the set of CORS headers
277
- */
278
- const commonHeaders: Record<string, string> = {
279
- Vary: "Origin",
280
- };
281
- if (allowCredentials) {
282
- commonHeaders["Access-Control-Allow-Credentials"] = "true";
283
- }
284
- if (exposedHeaders.length > 0) {
285
- commonHeaders["Access-Control-Expose-Headers"] = exposedHeaders.join(", ");
286
- }
287
-
288
- async function parseAllowedOrigins(request: Request): Promise<string[]> {
289
- return Array.isArray(allowedOrigins)
290
- ? allowedOrigins
291
- : await allowedOrigins(request);
292
- }
293
-
294
- // Helper function to check if origin is allowed (including wildcard subdomain matching)
295
- async function isAllowedOrigin(request: Request): Promise<boolean> {
296
- const requestOrigin = request.headers.get("origin");
297
- if (!requestOrigin) return false;
298
- return (await parseAllowedOrigins(request)).some((allowed) => {
299
- if (allowed === "*") return true;
300
- if (allowed === requestOrigin) return true;
301
- if (allowed.startsWith("*.")) {
302
- const wildcardDomain = allowed.slice(1); // ".bar.com"
303
- const rootDomain = allowed.slice(2); // "bar.com"
304
- try {
305
- const url = new URL(requestOrigin);
306
- return (
307
- url.protocol === "https:" &&
308
- (url.hostname.endsWith(wildcardDomain) ||
309
- url.hostname === rootDomain)
310
- );
311
- } catch {
312
- return false; // Invalid URL format
313
- }
314
- }
315
- return false;
316
- });
317
- }
318
-
319
- /**
320
- * Return our modified HTTP action
321
- */
322
- return httpActionGeneric(
323
- async (ctx: GenericActionCtx<any>, request: Request) => {
324
- if (debug) {
325
- console.log("CORS request", {
326
- path: request.url,
327
- origin: request.headers.get("origin"),
328
- headers: request.headers,
329
- method: request.method,
330
- body: request.body,
331
- });
332
- }
333
- const requestOrigin = request.headers.get("origin");
334
- const parsedAllowedOrigins = await parseAllowedOrigins(request);
335
-
336
- if (debug) {
337
- console.log("allowed origins", parsedAllowedOrigins);
338
- }
339
-
340
- // Handle origin matching
341
- let allowOrigins: string | null = null;
342
- if (parsedAllowedOrigins.includes("*") && !allowCredentials) {
343
- allowOrigins = "*";
344
- } else if (requestOrigin) {
345
- // Check if the request origin matches any of the allowed origins
346
- // (including wildcard subdomain matching if configured)
347
- if (await isAllowedOrigin(request)) {
348
- allowOrigins = requestOrigin;
349
- }
350
- }
351
-
352
- if (enforceAllowOrigins && !allowOrigins) {
353
- // Origin not allowed
354
- console.error(
355
- `Request from origin ${requestOrigin} blocked, missing from allowed origins: ${parsedAllowedOrigins.join()}`
356
- );
357
- return new Response(null, { status: 403 });
358
- }
359
- /**
360
- * OPTIONS has no handler and just returns headers
361
- */
362
- if (request.method === "OPTIONS") {
363
- const responseHeaders = new Headers({
364
- ...commonHeaders,
365
- "Access-Control-Allow-Origin": allowOrigins ?? "",
366
- "Access-Control-Allow-Methods": allowMethods,
367
- "Access-Control-Allow-Headers": allowedHeaders.join(", "),
368
- "Access-Control-Max-Age": browserCacheMaxAge.toString(),
369
- });
370
- if (debug) {
371
- console.log("CORS OPTIONS response headers", responseHeaders);
372
- }
373
- return new Response(null, {
374
- status: 204,
375
- headers: responseHeaders,
376
- });
377
- }
378
-
379
- /**
380
- * If the method is not OPTIONS, it must pass a handler
381
- */
382
- if (!originalHandler) {
383
- throw new Error("No PublicHttpAction provider to CORS handler");
384
- }
385
-
386
- /**
387
- * First, execute the original handler
388
- */
389
- const innerHandler = ("_handler" in originalHandler
390
- ? (originalHandler["_handler"] as PublicHttpAction)
391
- : originalHandler) as unknown as (
392
- ctx: GenericActionCtx<any>,
393
- request: Request
394
- ) => Promise<Response>;
395
- const originalResponse = await innerHandler(ctx, request);
396
-
397
- /**
398
- * Second, get a copy of the original response's headers
399
- */
400
- const newHeaders = new Headers(originalResponse.headers);
401
- newHeaders.set("Access-Control-Allow-Origin", allowOrigins ?? "");
402
-
403
- /**
404
- * Third, add or update our CORS headers
405
- */
406
- Object.entries(commonHeaders).forEach(([key, value]) => {
407
- newHeaders.set(key, value);
408
- });
409
-
410
- if (debug) {
411
- console.log("CORS response headers", newHeaders);
412
- }
413
-
414
- /**
415
- * Fourth, return the modified Response.
416
- * A Response object is immutable, so we create a new one to return here.
417
- */
418
- return new Response(originalResponse.body, {
419
- status: originalResponse.status,
420
- statusText: originalResponse.statusText,
421
- headers: newHeaders,
422
- });
423
- }
424
- );
425
- };
File without changes