Server additionalFields Error

When following this example from the docs: https://www.better-auth.com/docs/concepts/database#extending-core-schema, I get a few errors, based on the request I send.
// server auth.ts
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite",
schema: {
user,
session,
account,
verification,
},
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
usid: {
type: "string",
input: false,
required: false,
},
},
},
});
// server auth.ts
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: "sqlite",
schema: {
user,
session,
account,
verification,
},
}),
emailAndPassword: {
enabled: true,
},
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
usid: {
type: "string",
input: false,
required: false,
},
},
},
});
// also on server
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
}
})
// also on server
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
}
})
If i execute this I get ROLE_IS_REQUIRED error, but there was no Typescript error, which is not good. Also why do I get this error if I have defined a default value in my auth instance?
// request with role included
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
role: "user"
}
})
// request with role included
await auth.api.signUpEmail({
body: {
password: "password123",
name: 'test123',
role: "user"
}
})
When I execute this request, I get a long error, with this message: SQLite error: NOT NULL constraint failed: user.role,
Where is this going wrong?
Database | Better Auth
Learn how to use a database with Better Auth.
5 Replies
remakker
remakkerOP3mo ago
Oops, user error.
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
},
}
user: {
additionalFields: {
role: {
type: "string",
input: false,
default: "user",
required: true,
},
},
}
instead of
user: {
additionalFields: {
role: {
type: "string",
input: false,
defaultValue: "user",
required: true,
},
},
}
user: {
additionalFields: {
role: {
type: "string",
input: false,
defaultValue: "user",
required: true,
},
},
}
But the weird thing is that there was no type error in that. Here is a screenshot of the type of additionalFields, where default is highlighted pink. Is this some kind of typescript bug?
remakker
remakkerOP3mo ago
No description
melodyclue
melodyclue3mo ago
Database | Better Auth
Learn how to use a database with Better Auth.
remakker
remakkerOP3mo ago
No, I wrote default instead of defaultValue, so I did not follow the docs.
melodyclue
melodyclue3mo ago
oh, ok

Did you find this page helpful?