@convex-dev/better-auth 0.8.8 → 0.9.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 (116) hide show
  1. package/dist/commonjs/client/adapter.d.ts +8 -4
  2. package/dist/commonjs/client/adapter.d.ts.map +1 -1
  3. package/dist/commonjs/client/adapter.js +45 -8
  4. package/dist/commonjs/client/adapter.js.map +1 -1
  5. package/dist/commonjs/client/adapterUtils.d.ts +6 -6
  6. package/dist/commonjs/client/adapterUtils.d.ts.map +1 -1
  7. package/dist/commonjs/client/adapterUtils.js +17 -9
  8. package/dist/commonjs/client/adapterUtils.js.map +1 -1
  9. package/dist/commonjs/client/createSchema.d.ts +0 -5
  10. package/dist/commonjs/client/createSchema.d.ts.map +1 -1
  11. package/dist/commonjs/client/createSchema.js +0 -8
  12. package/dist/commonjs/client/createSchema.js.map +1 -1
  13. package/dist/commonjs/client/index.d.ts +76 -3367
  14. package/dist/commonjs/client/index.d.ts.map +1 -1
  15. package/dist/commonjs/client/index.js +100 -33
  16. package/dist/commonjs/client/index.js.map +1 -1
  17. package/dist/commonjs/component/adapter.d.ts +23 -21
  18. package/dist/commonjs/component/adapter.d.ts.map +1 -1
  19. package/dist/commonjs/component/adapter.js +1 -1
  20. package/dist/commonjs/component/adapter.js.map +1 -1
  21. package/dist/commonjs/component/adapterTest.d.ts +1 -1
  22. package/dist/commonjs/component/adapterTest.d.ts.map +1 -1
  23. package/dist/commonjs/component/schema.d.ts +16 -419
  24. package/dist/commonjs/component/schema.d.ts.map +1 -1
  25. package/dist/commonjs/component/schema.js +6 -196
  26. package/dist/commonjs/component/schema.js.map +1 -1
  27. package/dist/commonjs/plugins/convex/index.d.ts +10 -2
  28. package/dist/commonjs/plugins/convex/index.d.ts.map +1 -1
  29. package/dist/commonjs/plugins/convex/index.js +17 -1
  30. package/dist/commonjs/plugins/convex/index.js.map +1 -1
  31. package/dist/commonjs/plugins/cross-domain/index.d.ts +5 -0
  32. package/dist/commonjs/plugins/cross-domain/index.d.ts.map +1 -1
  33. package/dist/commonjs/plugins/cross-domain/index.js +8 -0
  34. package/dist/commonjs/plugins/cross-domain/index.js.map +1 -1
  35. package/dist/commonjs/react/index.js +3 -2
  36. package/dist/commonjs/react/index.js.map +1 -1
  37. package/dist/commonjs/react-start/index.d.ts.map +1 -1
  38. package/dist/commonjs/react-start/index.js +10 -2
  39. package/dist/commonjs/react-start/index.js.map +1 -1
  40. package/dist/commonjs/utils/index.d.ts +5 -5
  41. package/dist/commonjs/utils/index.d.ts.map +1 -1
  42. package/dist/commonjs/utils/index.js +9 -11
  43. package/dist/commonjs/utils/index.js.map +1 -1
  44. package/dist/esm/client/adapter.d.ts +8 -4
  45. package/dist/esm/client/adapter.d.ts.map +1 -1
  46. package/dist/esm/client/adapter.js +45 -8
  47. package/dist/esm/client/adapter.js.map +1 -1
  48. package/dist/esm/client/adapterUtils.d.ts +6 -6
  49. package/dist/esm/client/adapterUtils.d.ts.map +1 -1
  50. package/dist/esm/client/adapterUtils.js +17 -9
  51. package/dist/esm/client/adapterUtils.js.map +1 -1
  52. package/dist/esm/client/createSchema.d.ts +0 -5
  53. package/dist/esm/client/createSchema.d.ts.map +1 -1
  54. package/dist/esm/client/createSchema.js +0 -8
  55. package/dist/esm/client/createSchema.js.map +1 -1
  56. package/dist/esm/client/index.d.ts +76 -3367
  57. package/dist/esm/client/index.d.ts.map +1 -1
  58. package/dist/esm/client/index.js +100 -33
  59. package/dist/esm/client/index.js.map +1 -1
  60. package/dist/esm/component/adapter.d.ts +23 -21
  61. package/dist/esm/component/adapter.d.ts.map +1 -1
  62. package/dist/esm/component/adapter.js +1 -1
  63. package/dist/esm/component/adapter.js.map +1 -1
  64. package/dist/esm/component/adapterTest.d.ts +1 -1
  65. package/dist/esm/component/adapterTest.d.ts.map +1 -1
  66. package/dist/esm/component/schema.d.ts +16 -419
  67. package/dist/esm/component/schema.d.ts.map +1 -1
  68. package/dist/esm/component/schema.js +6 -196
  69. package/dist/esm/component/schema.js.map +1 -1
  70. package/dist/esm/plugins/convex/index.d.ts +10 -2
  71. package/dist/esm/plugins/convex/index.d.ts.map +1 -1
  72. package/dist/esm/plugins/convex/index.js +17 -1
  73. package/dist/esm/plugins/convex/index.js.map +1 -1
  74. package/dist/esm/plugins/cross-domain/index.d.ts +5 -0
  75. package/dist/esm/plugins/cross-domain/index.d.ts.map +1 -1
  76. package/dist/esm/plugins/cross-domain/index.js +8 -0
  77. package/dist/esm/plugins/cross-domain/index.js.map +1 -1
  78. package/dist/esm/react/index.js +3 -2
  79. package/dist/esm/react/index.js.map +1 -1
  80. package/dist/esm/react-start/index.d.ts.map +1 -1
  81. package/dist/esm/react-start/index.js +10 -2
  82. package/dist/esm/react-start/index.js.map +1 -1
  83. package/dist/esm/utils/index.d.ts +5 -5
  84. package/dist/esm/utils/index.d.ts.map +1 -1
  85. package/dist/esm/utils/index.js +9 -11
  86. package/dist/esm/utils/index.js.map +1 -1
  87. package/package.json +13 -8
  88. package/src/client/adapter.test.ts +94 -34
  89. package/src/client/adapter.ts +66 -14
  90. package/src/client/adapterUtils.ts +23 -8
  91. package/src/client/createSchema.ts +0 -8
  92. package/src/client/index.ts +124 -63
  93. package/src/component/_generated/api.d.ts +133 -1303
  94. package/src/component/adapter.ts +1 -0
  95. package/src/component/schema.ts +11 -214
  96. package/src/plugins/convex/index.ts +21 -1
  97. package/src/plugins/cross-domain/index.ts +8 -0
  98. package/src/react/index.tsx +3 -2
  99. package/src/react-start/index.ts +10 -2
  100. package/src/utils/index.ts +20 -10
  101. package/dist/commonjs/react/client.d.ts +0 -31
  102. package/dist/commonjs/react/client.d.ts.map +0 -1
  103. package/dist/commonjs/react/client.js +0 -96
  104. package/dist/commonjs/react/client.js.map +0 -1
  105. package/dist/commonjs/react/old-index.d.ts +0 -9
  106. package/dist/commonjs/react/old-index.d.ts.map +0 -1
  107. package/dist/commonjs/react/old-index.js +0 -15
  108. package/dist/commonjs/react/old-index.js.map +0 -1
  109. package/dist/esm/react/client.d.ts +0 -31
  110. package/dist/esm/react/client.d.ts.map +0 -1
  111. package/dist/esm/react/client.js +0 -96
  112. package/dist/esm/react/client.js.map +0 -1
  113. package/dist/esm/react/old-index.d.ts +0 -9
  114. package/dist/esm/react/old-index.d.ts.map +0 -1
  115. package/dist/esm/react/old-index.js +0 -15
  116. package/dist/esm/react/old-index.js.map +0 -1
@@ -21,7 +21,6 @@ import { type GenericId, Infer, v } from "convex/values";
21
21
  import { convexAdapter } from "./adapter";
22
22
  import { AdapterInstance, betterAuth } from "better-auth";
23
23
  import { asyncMap } from "convex-helpers";
24
- import { requireEnv } from "../utils";
25
24
  import { partial } from "convex-helpers/validators";
26
25
  import {
27
26
  adapterWhereValidator,
@@ -37,6 +36,7 @@ import semver from "semver";
37
36
  import defaultSchema from "../component/schema";
38
37
  import { getAuthTables } from "better-auth/db";
39
38
  import { api } from "../component/_generated/api";
39
+ import { SetOptional } from "type-fest";
40
40
 
41
41
  export { convexAdapter };
42
42
 
@@ -70,7 +70,7 @@ const whereValidator = (
70
70
  ...Object.keys(schema.tables[tableName].validator.fields).map((field) =>
71
71
  v.literal(field)
72
72
  ),
73
- v.literal("id")
73
+ v.literal("_id")
74
74
  ),
75
75
  operator: v.optional(
76
76
  v.union(
@@ -80,6 +80,7 @@ const whereValidator = (
80
80
  v.literal("gte"),
81
81
  v.literal("eq"),
82
82
  v.literal("in"),
83
+ v.literal("not_in"),
83
84
  v.literal("ne"),
84
85
  v.literal("contains"),
85
86
  v.literal("starts_with"),
@@ -123,6 +124,16 @@ export const createApi = <
123
124
  ) => {
124
125
  const betterAuthSchema = getAuthTables(getStaticAuth(createAuth).options);
125
126
  return {
127
+ migrationRemoveUserId: mutationGeneric({
128
+ args: {
129
+ userId: v.string(),
130
+ },
131
+ handler: async (ctx, args) => {
132
+ await ctx.db.patch(args.userId as GenericId<"user">, {
133
+ userId: undefined,
134
+ });
135
+ },
136
+ }),
126
137
  create: mutationGeneric({
127
138
  args: {
128
139
  input: v.union(
@@ -239,8 +250,8 @@ export const createApi = <
239
250
  args.onUpdateHandle as FunctionHandle<"mutation">,
240
251
  {
241
252
  model: args.input.model,
242
- oldDoc: doc,
243
253
  newDoc: updatedDoc,
254
+ oldDoc: doc,
244
255
  }
245
256
  );
246
257
  }
@@ -306,8 +317,8 @@ export const createApi = <
306
317
  args.onUpdateHandle as FunctionHandle<"mutation">,
307
318
  {
308
319
  model: args.input.model,
309
- oldDoc: doc,
310
320
  newDoc: await ctx.db.get(doc._id as GenericId<string>),
321
+ oldDoc: doc,
311
322
  }
312
323
  );
313
324
  }
@@ -429,7 +440,13 @@ export const createClient = <
429
440
  DataModel extends GenericDataModel,
430
441
  Schema extends SchemaDefinition<GenericSchema, true> = typeof defaultSchema,
431
442
  >(
432
- component: UseApi<typeof api>,
443
+ component: {
444
+ adapter: SetOptional<
445
+ UseApi<typeof api>["adapter"],
446
+ "migrationRemoveUserId"
447
+ >;
448
+ adapterTest?: UseApi<typeof api>["adapterTest"];
449
+ },
433
450
  config?: {
434
451
  local?: {
435
452
  schema?: Schema;
@@ -451,7 +468,7 @@ export const createClient = <
451
468
  model: "user",
452
469
  where: [
453
470
  {
454
- field: "id",
471
+ field: "_id",
455
472
  value: identity.subject,
456
473
  },
457
474
  ],
@@ -461,34 +478,59 @@ export const createClient = <
461
478
  }
462
479
  return doc;
463
480
  };
481
+
482
+ const getHeaders = async (ctx: GenericCtx<DataModel>) => {
483
+ const identity = await ctx.auth.getUserIdentity();
484
+ if (!identity) {
485
+ return new Headers();
486
+ }
487
+ const session = await ctx.runQuery(component.adapter.findOne, {
488
+ model: "session",
489
+ where: [
490
+ {
491
+ field: "_id",
492
+ value: identity.sessionId as string,
493
+ },
494
+ ],
495
+ });
496
+ return new Headers({
497
+ ...(session?.token ? { authorization: `Bearer ${session.token}` } : {}),
498
+ ...(session?.ipAddress
499
+ ? { "x-forwarded-for": session.ipAddress as string }
500
+ : {}),
501
+ });
502
+ };
503
+
464
504
  return {
465
505
  component,
466
506
  adapter: (ctx: GenericCtx<DataModel>) =>
467
- convexAdapter<DataModel, typeof ctx, Schema>(ctx, component, config),
468
- getHeaders: async (ctx: GenericQueryCtx<DataModel>) => {
469
- const identity = await ctx.auth.getUserIdentity();
470
- if (!identity) {
471
- return new Headers();
472
- }
473
- const session = await ctx.runQuery(component.adapter.findOne, {
474
- model: "session",
475
- where: [
476
- {
477
- field: "id",
478
- value: identity.sessionId as string,
479
- },
480
- ],
481
- });
482
- return new Headers({
483
- ...(session?.token ? { authorization: `Bearer ${session.token}` } : {}),
484
- ...(session?.ipAddress
485
- ? { "x-forwarded-for": session.ipAddress as string }
486
- : {}),
487
- });
488
- },
507
+ convexAdapter<DataModel, typeof ctx, Schema>(ctx, component, {
508
+ ...config,
509
+ debugLogs: config?.verbose,
510
+ }),
489
511
 
512
+ getAuth: async <T extends CreateAuth<DataModel>>(
513
+ createAuth: T,
514
+ ctx: GenericCtx<DataModel>
515
+ ) => ({
516
+ auth: createAuth(ctx) as ReturnType<T>,
517
+ headers: await getHeaders(ctx),
518
+ }),
519
+
520
+ getHeaders,
521
+
522
+ /**
523
+ * Returns the current user or null if the user is not found
524
+ * @param ctx - The Convex context
525
+ * @returns The user or null if the user is not found
526
+ */
490
527
  safeGetAuthUser,
491
528
 
529
+ /**
530
+ * Returns the current user.
531
+ * @param ctx - The Convex context
532
+ * @returns The user or throws an error if the user is not found
533
+ */
492
534
  getAuthUser: async (ctx: GenericCtx<DataModel>) => {
493
535
  const user = await safeGetAuthUser(ctx);
494
536
  if (!user) {
@@ -497,15 +539,29 @@ export const createClient = <
497
539
  return user;
498
540
  },
499
541
 
542
+ /**
543
+ * Returns a user by their Better Auth user id.
544
+ * @param ctx - The Convex context
545
+ * @param id - The Better Auth user id
546
+ * @returns The user or null if the user is not found
547
+ */
500
548
  getAnyUserById: async (ctx: GenericCtx<DataModel>, id: string) => {
501
549
  return (await ctx.runQuery(component.adapter.findOne, {
502
550
  model: "user",
503
- where: [{ field: "id", value: id }],
551
+ where: [{ field: "_id", value: id }],
504
552
  })) as BetterAuthDataModel["user"]["document"] | null;
505
553
  },
506
554
 
507
555
  // Replaces 0.7 behavior of returning a new user id from
508
- // onCreateUser
556
+ // onCreateUser, deprecated in 0.9
557
+ /**
558
+ * Replaces 0.7 behavior of returning a new user id from
559
+ * onCreateUser
560
+ * @param ctx - The Convex context
561
+ * @param authId - The Better Auth user id
562
+ * @param userId - The app user id
563
+ * @deprecated in 0.9
564
+ */
509
565
  setUserId: async (
510
566
  ctx: GenericMutationCtx<DataModel>,
511
567
  authId: string,
@@ -514,12 +570,46 @@ export const createClient = <
514
570
  await ctx.runMutation(component.adapter.updateOne, {
515
571
  input: {
516
572
  model: "user",
517
- where: [{ field: "id", value: authId }],
573
+ where: [{ field: "_id", value: authId }],
518
574
  update: { userId },
519
575
  },
520
576
  });
521
577
  },
522
578
 
579
+ /**
580
+ * Temporary method to simplify 0.9 migration, gets a user by `userId` field
581
+ * @param ctx - The Convex context
582
+ * @param userId - The app user id
583
+ * @returns The user or null if the user is not found
584
+ */
585
+ migrationGetUser: async (
586
+ ctx: GenericMutationCtx<DataModel>,
587
+ userId: string
588
+ ) => {
589
+ return (await ctx.runQuery(component.adapter.findOne, {
590
+ model: "user",
591
+ where: [{ field: "userId", value: userId }],
592
+ })) as BetterAuthDataModel["user"]["document"] | null;
593
+ },
594
+
595
+ /**
596
+ * Temporary method to simplify 0.9 migration, removes the `userId` field
597
+ * from the Better Auth user record
598
+ * @param ctx - The Convex context
599
+ * @param userId - The app user id
600
+ */
601
+ migrationRemoveUserId: async (
602
+ ctx: GenericMutationCtx<DataModel>,
603
+ userId: string
604
+ ) => {
605
+ if (!component.adapter.migrationRemoveUserId) {
606
+ throw new Error("migrationRemoveUserId not found");
607
+ }
608
+ await ctx.runMutation(component.adapter.migrationRemoveUserId, {
609
+ userId,
610
+ });
611
+ },
612
+
523
613
  triggersApi: () => ({
524
614
  onCreate: internalMutationGeneric({
525
615
  args: {
@@ -539,8 +629,8 @@ export const createClient = <
539
629
  handler: async (ctx, args) => {
540
630
  await config?.triggers?.[args.model]?.onUpdate?.(
541
631
  ctx,
542
- args.oldDoc,
543
- args.newDoc
632
+ args.newDoc,
633
+ args.oldDoc
544
634
  );
545
635
  },
546
636
  }),
@@ -592,7 +682,7 @@ export const createClient = <
592
682
  path: "/.well-known/openid-configuration",
593
683
  method: "GET",
594
684
  handler: httpActionGeneric(async () => {
595
- const url = `${requireEnv("CONVEX_SITE_URL")}${path}/convex/.well-known/openid-configuration`;
685
+ const url = `${process.env.CONVEX_SITE_URL}${path}/convex/.well-known/openid-configuration`;
596
686
  return Response.redirect(url);
597
687
  }),
598
688
  });
@@ -667,35 +757,6 @@ export const createClient = <
667
757
 
668
758
  /* Type utils follow */
669
759
 
670
- /**
671
- * @deprecated Use `QueryCtx` from _generated/server instead
672
- */
673
- export type RunQueryCtx = {
674
- runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
675
- };
676
-
677
- /**
678
- * @deprecated Use `MutationCtx` from _generated/server instead
679
- */
680
- export type RunMutationCtx = {
681
- runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
682
- runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
683
- };
684
-
685
- /**
686
- * @deprecated Use `ActionCtx` from _generated/server instead
687
- */
688
- export type RunActionCtx = {
689
- runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
690
- runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
691
- runAction: GenericActionCtx<GenericDataModel>["runAction"];
692
- };
693
-
694
- /**
695
- * @deprecated
696
- */
697
- export type RunCtx = RunQueryCtx | RunMutationCtx | RunActionCtx;
698
-
699
760
  export type OpaqueIds<T> =
700
761
  T extends GenericId<infer _T>
701
762
  ? string