Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Better Auth · Pipe

databaseHooks

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.

How it works

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.

Syntax

betterAuth({ databaseHooks: { resource: { operation: { before: async (entity, ctx) => ..., after: async (entity, ctx) => ... } } } })

Parameters

NameTypeDescription
databaseHooksobjectConfiguration object containing lifecycle callbacks grouped by resource and database operation.
userobjectThe pending or completed entity supplied to the lifecycle callback.
ctxobjectOptional callback context available when the hook signature includes it.

Returns

A before callback may return an object containing replacement data; an after callback can perform follow-up work and normally returns nothing.

Key points

  • databaseHooks attach application callbacks to Better Auth's core database lifecycle.
  • A user.create.before callback can transform the pending user record before it is stored.
  • Return a data wrapper from a before callback when the operation should persist modified entity data.
  • A user.create.after callback receives the created user for post-persistence work.

Example: Add user creation lifecycle hooks

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.

When to use it

Enrich records before creation

A user must receive application-specific attributes during signup.

Run post-write actions

The application needs work after authentication data is persisted.

Reject disallowed user creation

Signup eligibility depends on application policy.

Interview questions

QWhat are Better Auth database hooks used for?

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.

QHow do before and after callbacks differ?

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.

QHow is a user creation hook configured?

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.

  • Hooks
  • User & Accounts
  • database
  • betterAuth

Source: Options | Better Auth. Checked 2026-10-15.