Skip to content

Commit 7927339

Browse files
committed
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.
1 parent 753d7e0 commit 7927339

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/adapterUtils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,20 @@ const isUniqueField = (
6262
model: string,
6363
field: string
6464
) => {
65-
const fields =
66-
betterAuthSchema[model as keyof typeof betterAuthSchema].fields;
67-
68-
if (!fields) {
65+
// Map Convex table name (e.g., "users") to Better Auth model key (e.g., "user")
66+
// by finding the key where betterAuthSchema[key].modelName === model
67+
const betterAuthModel =
68+
Object.keys(betterAuthSchema).find(
69+
(key) => betterAuthSchema[key as keyof typeof betterAuthSchema].modelName === model
70+
) || model;
71+
const modelSchema =
72+
betterAuthSchema[betterAuthModel as keyof typeof betterAuthSchema];
73+
74+
if (!modelSchema?.fields) {
6975
return false;
7076
}
7177

72-
return Object.entries(fields)
78+
return Object.entries(modelSchema.fields)
7379
.filter(([, value]) => value.unique)
7480
.map(([key]) => key)
7581
.includes(field);

0 commit comments

Comments
 (0)