Built-in feature integration
Add a built-in authentication feature such as two-factor authentication.
Better Auth · Pipe
Better Auth plugins extend the framework with authentication features and custom behavior. Register server plugins in the auth configuration and client plugins when creating the client.
A Better Auth plugin extends the authentication system with additional behavior. Server plugins are registered in the auth configuration, while client plugins are registered when creating the client; many features need both sides configured.
plugins: [plugin(options)]import { betterAuth } from "better-auth";
import { twoFactor } from "better-auth/plugins";
export const auth = betterAuth({
plugins: [twoFactor()]
});Adds a built-in plugin to the server authentication configuration.
Add a built-in authentication feature such as two-factor authentication.
Expose plugin-specific routes or operations.
Add tables or fields required by an extension.
Apply route-specific or request-wide behavior.
question: How do you add a server plugin? Include the plugin instance in the plugins array of the Better Auth configuration. The plugin is initialized with the options passed to it.
Follow-up: question: Can a plugin be client-side only? answer: Yes, plugins can be server-side, client-side, or both, although many require both halves.
A plugin can define endpoints, extend database schemas, apply middleware or hooks, affect all requests or responses, and add rate-limit rules. These capabilities let it add authentication features or customize existing behavior.
Follow-up: Which mechanism should target a specific route? Use a hook; use middleware when matching a group of routes.
A custom server plugin is an object satisfying the BetterAuthPlugin interface. Its only required property is id, which uniquely identifies the plugin.
Follow-up: What must a client plugin provide? It is added when creating the client and supplies frontend-facing APIs for the server plugin.
BetterAuthPluginendpointsschemasmiddlewarehooksonRequestonResponserate-limit