Enrich records before creation
A user must receive application-specific attributes during signup.
Better Auth · Pipe
Better Auth databaseHooks provide before and after callbacks around core database operations. Use them to transform records before persistence or run follow-up logic after an operation completes.
Add lifecycle callbacks to Better Auth database operations. For a user creation, the before callback can return changed data, while the after callback runs after the record is created; the same structure supports other documented resources and operations.
betterAuth({ databaseHooks: { resource: { operation: { before: async (entity, ctx) => ..., after: async (entity, ctx) => ... } } } })| Name | Type | Description |
|---|---|---|
databaseHooks | object | Configuration object containing lifecycle callbacks grouped by resource and database operation. |
user | object | The pending or completed entity supplied to the lifecycle callback. |
ctx | object | Optional callback context available when the hook signature includes it. |
A before callback may return an object containing replacement data; an after callback can perform follow-up work and normally returns nothing.
import { betterAuth } from "better-auth";
export const auth = betterAuth({
databaseHooks: {
user: {
create: {
before: async (user) => ({
data: { ...user, customField: "value" },
}),
after: async (user) => {
console.log("Created user:", user.id);
},
},
},
},
});This config adds a field before a user is created and runs follow-up logic after creation.
A user must receive application-specific attributes during signup.
The application needs work after authentication data is persisted.
Signup eligibility depends on application policy.
Database hooks are lifecycle callbacks attached under databaseHooks. They let application code run before or after core database operations, such as creating a user. A before callback can return replacement data for the operation.
Follow-up: How would you reject a signup? Throw an appropriate Better Auth API error from the user.create.before callback when the user does not meet the application's policy.
The before callback executes before persistence and can modify the object by returning a data wrapper. The after callback executes after the operation completes and is suitable for follow-up work such as logging or provisioning related data.
Follow-up: What should be checked when a create hook fails? Inspect the hook for thrown errors or invalid returned data, then verify the database schema and required fields.
Configure hooks inside the resource and operation path, for example databaseHooks.user.create.before or .after. The callback receives the entity, and a before callback may return an object containing updated data.
Follow-up: Can endpoint hooks replace database hooks? Not always: database hooks target core database operations, including calls made through authentication flows, whereas endpoint hooks target request handling.
HooksUser & AccountsdatabasebetterAuth