Convex + Better Auth
Migrations

Migrate to 0.9

Migrate to @convex-dev/better-auth@0.9

Breaking changes

This release includes breaking changes. Not following the migration steps correctly can lead to unexpected behavior.

Upgrade component

Install dependencies

Update Better Auth and the component.

npm install @convex-dev/better-auth@0.9
npm install better-auth@1.3.27 --save-exact

Regenerate schema (Local Install only)

Whenever updating Better Auth w/ Local Install, regenerate the schema.

cd convex/betterAuth
npx @better-auth/cli generate -y

Update onUpdate triggers

Breaking change!

Any triggers using the onUpdate hook should be updated to expect the new doc as the second parameter, and the old doc as the third parameter. The old doc parameter can be left out of the signature if not needed.

convex/auth.ts
export const authComponent = createClient<DataModel>(components.betterAuth, {
  authFunctions,
  triggers: {
    user: {
      onUpdate: async (ctx, oldDoc, newDoc) => { 
      onUpdate: async (ctx, newDoc, oldDoc) => { 
        // oldDoc can be left out of the signature if not needed
      },
    },
  },
});

Use _id in database adapter

This isn't technically breaking since the database adapter api is not a part of the documented api surface, but any adapter usage referencing the id field should be updated to use _id instead.

Using the adapter directly is not unsafe, but it is not recommended. Results are not typesafe, and breaking changes may occur.

convex/auth.ts
export const getUserById = query({
  args: { authId: v.string() },
  handler: async (ctx, args) => {
    const user = await ctx.runQuery(components.betterAuth.adapter.findOne, {
      model: "user",
      where: [{ field: "id", value: args.authId }], 
      where: [{ field: "_id", value: args.authId }], 
    });
    return user;
  },
});

Migrate away from user.userId support

This step is not required for 0.9, but will be required in a future release.

Storing your app user id in the Better Auth user table is no longer required, and built in support for it is deprecated.