Sign-up: posting fields to other tables
I've added additional fields to 'user' that all post fine, but I also have another table that takes the user.id as a foreign key and one field that authClient.signUp.email sends belongs to this table. I'm using a Drizzle Schema for the db, so the schema is at least known by BA.
database: drizzleAdapter(db, {
provider: 'pg',
schema: dbschema, // contains all tables
}),
I've added the table and field in question as well to auth config
otherTable: {
additionalFields: {
fieldName: {
type: "string",
input: true,
},
}
},
This field is not posted to the db, nor do I see it handled in BA debug level logging. It appears as if it's silently ignored.
Do I need to look at Database Hooks or a plugin that hooks into onSignUp? The docs imply as much, https://www.better-auth.com/docs/concepts/database#plugins-schema, but I might be missing something here.Database | Better Auth
Learn how to use a database with Better Auth.
3 Replies
Solution
I recommend just creating a row in that other table in a database hook
Thanks. Given that BetterAuth may be a cornerstone in user schemas, is it on the horizon handling directly related tables referencing user.id in the same way addionalFields work with the core schema? It's very simple to config, but is ignored. Or is it given that the developer ought to replace the function in the backend or add on plugins?
In my case I choose the authClient for sign-up and needed a table with another focus than user.
The reason why we don't allow configuring other tables in the auth config is because we determine schema configurations based on where they come from, for example
user
, session
, account
& verification
will always need to exist and is core to Better-Auth, but plugins adding new tables usually have schema configuration in the plugin options - since it's not core to BA it's not in the auth config directly.