From 7927339491eede79664309de3401a6cfa237f031 Mon Sep 17 00:00:00 2001 From: Armand <136462994+thuillart@users.noreply.github.com> Date: Wed, 12 Nov 2025 14:02:26 +0100 Subject: [PATCH 1/2] fix: map Convex table names to Better Auth model keys in isUniqueField Fixes a bug where isUniqueField tries to access the Better Auth schema using Convex table names (e.g., "users") directly, but the schema object uses Better Auth's internal model names as keys (e.g., "user"). This causes the error: "Cannot read properties of undefined (reading 'fields')" The fix maps Convex table names to Better Auth internal model names by finding the key where betterAuthSchema[key].modelName === model, ensuring compatibility with custom modelName values in Better Auth configuration. --- src/adapterUtils.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/adapterUtils.ts b/src/adapterUtils.ts index 3efc3ea..938749a 100644 --- a/src/adapterUtils.ts +++ b/src/adapterUtils.ts @@ -62,14 +62,20 @@ const isUniqueField = ( model: string, field: string ) => { - const fields = - betterAuthSchema[model as keyof typeof betterAuthSchema].fields; - - if (!fields) { + // Map Convex table name (e.g., "users") to Better Auth model key (e.g., "user") + // by finding the key where betterAuthSchema[key].modelName === model + const betterAuthModel = + Object.keys(betterAuthSchema).find( + (key) => betterAuthSchema[key as keyof typeof betterAuthSchema].modelName === model + ) || model; + const modelSchema = + betterAuthSchema[betterAuthModel as keyof typeof betterAuthSchema]; + + if (!modelSchema?.fields) { return false; } - return Object.entries(fields) + return Object.entries(modelSchema.fields) .filter(([, value]) => value.unique) .map(([key]) => key) .includes(field); From e7c102d85bb6a2092d5bb47e942fb5290cb96ead Mon Sep 17 00:00:00 2001 From: Ziad Beyens Date: Wed, 12 Nov 2025 14:08:42 +0100 Subject: [PATCH 2/2] Create eleven-spiders-wonder.md --- .changeset/eleven-spiders-wonder.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eleven-spiders-wonder.md diff --git a/.changeset/eleven-spiders-wonder.md b/.changeset/eleven-spiders-wonder.md new file mode 100644 index 0000000..49b693d --- /dev/null +++ b/.changeset/eleven-spiders-wonder.md @@ -0,0 +1,5 @@ +--- +"better-auth-convex": patch +--- + +fix: map Convex table names to Better Auth model keys in isUniqueField