Typescript - database hook type inference for additional fields

This is my current auth.ts:
export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});
export const auth = betterAuth({
user: {
additionalFields: {
role: {
fieldName: 'role',
type: 'string',
required: true,
input: false,
},
},
},
hooks: {
before: createAuthMiddleware(async (ctx) => {
if (ctx.path !== '/sign-up/email') {
return;
}
if (ctx.body?.registrationToken !== REGISTRATION_TOKEN) {
throw new APIError('UNAUTHORIZED', {
message: 'Incorrect registration token provided. Please contact your server admin',
});
}
}),
},
databaseHooks: {
user: {
create: {
before: async (user) => {
const adminUserExists = await db.query.usersTable.findFirst({
where: (t, { eq }) => eq(t.role, 'ADMIN'),
});
return {
data: {
...user,
role: adminUserExists ? 'USER' : 'ADMIN',
},
};
},
},
},
},
emailAndPassword: {
enabled: true,
minPasswordLength: 4,
maxPasswordLength: 100,
autoSignIn: true,
},
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
user: schema.usersTable,
session: schema.sessionsTable,
account: schema.accountsTable,
verification: schema.verificationsTable,
},
}),
});
In the database -> user -> create -> before hook, despite setting the "role" additional field as required, there is no issue if I were to remove the "role" field, i.e.
databaseHooks: {
user: {
create: {
before: async (user) => {
return {
data: {
...user,
}
}
}
}
databaseHooks: {
user: {
create: {
before: async (user) => {
return {
data: {
...user,
}
}
}
}
i.e. the role "type" is not picked up at all. I'm also facing a separate type issue client-side, which I've posted here: https://discord.com/channels/1288403910284935179/1323671049413333033/1323857938875420734 Am I doing something wrong or is this an issue with the type system?
7 Replies
Unknown User
Unknown User4mo ago
Message Not Public
Sign In & Join Server To View
Raiyan
Raiyan2w ago
no updates on this?
KiNFiSH
KiNFiSH2w ago
rather than the type . is it working fine like it gets passed ?
Megamind
Megamind2w ago
i am doing similiar. the feilds are not added
Glen Kurio
Glen Kurio2w ago
For type to be picked up you need to infer it: in auth.ts: export type User = typeof auth.$Infer.Session.user; in auth-client:
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_APP_URL,
plugins: [
stripeClient({
subscription: true, //if you want to enable subscription management
}),
inferAdditionalFields<typeof auth>(),
],
});
export const authClient = createAuthClient({
baseURL: process.env.NEXT_PUBLIC_APP_URL,
plugins: [
stripeClient({
subscription: true, //if you want to enable subscription management
}),
inferAdditionalFields<typeof auth>(),
],
});
and what do you mean by not added ? To db? you need to generate the schema after you modified the auth.ts config: npx @better-auth/cli generate or migrate (works with kysely only)
Megamind
Megamind2w ago
I am using the organizations plugin. And trying to add addition fields to the members. I have updated the tables manually with prisma schema. The additional fields are not being added to the db.
KiNFiSH
KiNFiSH2w ago
you have to generate and make sure to add ts infereence on example here - https://www.better-auth.com/docs/concepts/typescript#additional-fields
TypeScript | Better Auth
Better Auth TypeScript integration.

Did you find this page helpful?