better-auth-plugins

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Better Auth Plugins

Better Auth 插件系统

Goals

目标

  • Add server plugins to extend auth features.
  • Add client plugins for matching client methods.
  • Apply schema changes when plugins add tables.
  • 添加服务端插件以扩展认证功能。
  • 添加对应的客户端插件以匹配服务端方法。
  • 当插件添加数据表时应用架构变更。

Quick start

快速开始

  1. Import the plugin from
    better-auth/plugins
    and add it to
    plugins
    .
  2. Run migrations (
    generate
    or
    migrate
    ) when required.
  3. Add the client plugin from
    better-auth/client/plugins
    .
  1. better-auth/plugins
    导入插件并添加到
    plugins
    配置中。
  2. 必要时运行迁移命令(
    generate
    migrate
    )。
  3. better-auth/client/plugins
    添加客户端插件。

Example: Two-factor authentication

示例:双因素认证

ts
import { betterAuth } from "better-auth";
import { twoFactor } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [twoFactor()],
});
ts
import { createAuthClient } from "better-auth/client";
import { twoFactorClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
  plugins: [twoFactorClient({ twoFactorPage: "/two-factor" })],
});
ts
import { betterAuth } from "better-auth";
import { twoFactor } from "better-auth/plugins";

export const auth = betterAuth({
  plugins: [twoFactor()],
});
ts
import { createAuthClient } from "better-auth/client";
import { twoFactorClient } from "better-auth/client/plugins";

export const authClient = createAuthClient({
  plugins: [twoFactorClient({ twoFactorPage: "/two-factor" })],
});

Migration reminder

迁移提醒

Run the CLI when a plugin adds tables:
bash
npx @better-auth/cli generate
bash
npx @better-auth/cli migrate
当插件添加数据表时运行CLI命令:
bash
npx @better-auth/cli generate
bash
npx @better-auth/cli migrate

Guardrails

注意事项

  • Add server and client plugins together to keep APIs aligned.
  • Keep
    nextCookies
    (if used) last in the server plugin list.
  • Review plugin docs for required schema and env variables.
  • 同时添加服务端和客户端插件以保持API对齐。
  • 如果使用
    nextCookies
    ,请将其放在服务端插件列表的最后。
  • 查看插件文档以了解所需的架构和环境变量。

References

参考资料

  • toolchains/platforms/auth/better-auth/better-auth-plugins/references/plugins-index.md
  • toolchains/platforms/auth/better-auth/better-auth-plugins/references/plugins-index.md