Pre-validation and authorization
Validate signup or sign-in input before the endpoint runs and reject invalid requests.
Better Auth · Pipe
Better Auth hooks let you run custom middleware before or after an auth endpoint. They support request and response customization, validation, cookies, errors, auth context access, and background work.
Hooks attach custom logic to Better Auth's request lifecycle. A before hook can inspect or alter an incoming request, validate data, stop processing early, or throw an error; an after hook can inspect or modify the response and perform follow-up work.
betterAuth({ hooks: { before: createAuthMiddleware(async (ctx) => { /* ... */ }), after: createAuthMiddleware(async (ctx) => { /* ... */ }) } })| Name | Type | Description |
|---|---|---|
hooks | object | Request lifecycle configuration containing before and after middleware handlers. |
ctx | AuthMiddlewareContext | Middleware context for the current auth request and endpoint. |
before | AuthMiddleware | Hook middleware executed before endpoint processing. |
after | AuthMiddleware | Hook middleware executed after endpoint processing. |
A hook may return a response/context for early handling; otherwise endpoint processing continues. After hooks can modify the resulting response.
import { betterAuth } from "better-auth";
import { createAuthMiddleware } from "better-auth/api";
export const auth = betterAuth({
hooks: {
before: createAuthMiddleware(async (ctx) => {
console.log("Request path:", ctx.path);
}),
after: createAuthMiddleware(async (ctx) => {
console.log("Response:", ctx.context.returned);
}),
},
});Registers middleware that runs before and after Better Auth endpoint processing.
Validate signup or sign-in input before the endpoint runs and reject invalid requests.
Add or update cookies using the request context's cookie helpers.
Adjust endpoint output or trigger work after a successful operation.
Hooks let custom logic participate in Better Auth's endpoint lifecycle without implementing a separate endpoint or full plugin. The configuration exposes before and after handlers, and each handler is created with createAuthMiddleware.
Follow-up: How would you prevent an endpoint from continuing? Put the validation in a before hook and return early or throw APIError when the request is invalid.
A before hook executes before the endpoint and can modify the request, validate data, return early, or perform authorization logic. An after hook executes after the endpoint and is suited to response changes and background work.
Follow-up: Where should response-dependent logic go? In an after hook, because it runs after endpoint execution and can access the returned response through the auth context.
The hook context exposes request/response helpers and an auth-related context object. For example, ctx.json sends JSON, cookie helpers set or read cookies, and APIError creates an error with a chosen status and message.
Follow-up: How can a hook interact with the session created during processing? Use the nested auth context, which contains values such as the newly created session and cookie configuration.
createAuthMiddlewareAPIErrorctx.jsonctx.setCookiectx.setSignedCookiectx.getCookiectx.getSignedCookiedatabaseHooks