命令行接口
Better Auth 内置了命令行工具(CLI),可帮助你管理数据库模式、初始化项目、为应用生成密钥,并收集有关设置的诊断信息。
🌐 Better Auth comes with a built-in CLI to help you manage the database schemas, initialize your project, generate a secret key for your application, and gather diagnostic information about your setup.
生成
🌐 Generate
generate 命令会创建 Better Auth 所需的架构。如果你正在使用像 Prisma 或 Drizzle 这样的数据库适配器,该命令会为你的 ORM 生成正确的架构。如果你使用内置的 Kysely 适配器,它将生成一个可以直接在数据库上运行的 SQL 文件。
🌐 The generate command creates the schema required by Better Auth. If you're using a database adapter like Prisma or Drizzle, this command will generate the right schema for your ORM. If you're using the built-in Kysely adapter, it will generate an SQL file you can run directly on your database.
npx @better-auth/cli@latest generate选项
🌐 Options
--output- 用于指定生成的 schema 保存位置。对于 Prisma,它将保存在 prisma/schema.prisma。对于 Drizzle,它会保存到项目根目录下的 schema.ts。对于 Kysely,它是一个 SQL 文件,保存为项目根目录下的 schema.sql。--config- 你的 Better Auth 配置文件的路径。默认情况下,CLI 会在 ./、./utils、./lib,或src目录下的这些目录中查找 auth.ts 文件。--yes- 跳过确认提示,直接生成架构。
迁移
🌐 Migrate
migrate 命令会将 Better Auth 的 schema 直接应用到你的数据库。如果你使用的是内置的 Kysely 适配器,这个命令是可用的。对于其他适配器,你需要使用你的 ORM 的迁移工具来应用 schema。
🌐 The migrate command applies the Better Auth schema directly to your database. This is available if you're using the built-in Kysely adapter. For other adapters, you'll need to apply the schema using your ORM's migration tool.
npx @better-auth/cli@latest migrate选项
🌐 Options
--config- 你的 Better Auth 配置文件的路径。默认情况下,CLI 会在 ./、./utils、./lib,或src目录下的这些目录中查找 auth.ts 文件。--yes- 跳过确认提示,直接应用架构。
使用非默认架构的 PostgreSQL?
migrate 命令会自动检测你配置的 search_path 并在正确的 schema 中创建表。有关配置详情,请参见 PostgreSQL 适配器文档。
初始化
🌐 Init
init 命令允许你在项目中初始化 Better Auth。
🌐 The init command allows you to initialize Better Auth in your project.
npx @better-auth/cli@latest init选项
🌐 Options
--name- 你的应用的名称。(默认为你package.json中的name属性)。--framework- 你的代码库正在使用的框架。目前,唯一支持的框架是Next.js。--plugins- 你想要使用的插件。你可以通过逗号分隔来指定多个插件。--database- 你想要使用的数据库。目前唯一支持的数据库是SQLite。--package-manager- 你想要使用的包管理器。目前唯一支持的包管理器是npm、pnpm、yarn、bun(默认为你用来初始化 CLI 的管理器)。
信息
🌐 Info
info 命令提供有关你的 Better Auth 设置和环境的诊断信息。对于调试以及在寻求支持时共享信息非常有用。
🌐 The info command provides diagnostic information about your Better Auth setup and environment. Useful for debugging and sharing when seeking support.
npx @better-auth/cli@latest info输出
🌐 Output
该命令显示:
🌐 The command displays:
- 系统:操作系统、CPU、内存、Node.js 版本
- 包管理器:检测到管理器及其版本
- 更佳认证:版本和配置(敏感数据已自动屏蔽)
- 框架:检测到的框架(Next.js、React、Vue 等)
- 数据库:数据库客户端和 ORM(Prisma、Drizzle 等)
选项
🌐 Options
--config- 指向你的 Better Auth 配置文件的路径--json- 以 JSON 格式输出,便于共享或程序使用
例子
🌐 Examples
# Basic usage
npx @better-auth/cli@latest info
# Custom config path
npx @better-auth/cli@latest info --config ./config/auth.ts
# JSON output
npx @better-auth/cli@latest info --json > auth-info.json敏感数据,如机密、API 密钥和数据库 URL,会被自动替换为 [REDACTED],以便安全共享。
🌐 Sensitive data like secrets, API keys, and database URLs are automatically replaced with [REDACTED] for safe sharing.
秘密
🌐 Secret
该 CLI 还提供了一种为你的 Better Auth 实例生成密钥的方法。
🌐 The CLI also provides a way to generate a secret key for your Better Auth instance.
npx @better-auth/cli@latest secret常见问题
🌐 Common Issues
错误:找不到模块 X
如果你看到此错误,这意味着 CLI 无法解析你在 Better Auth 配置文件中导入的模块。我们正在修复许多此类问题,但与此同时,你可以尝试以下方法:
🌐 If you see this error, it means the CLI can't resolve imported modules in your Better Auth config file. We are working on a fix for many of these issues, but in the meantime, you can try the following:
- 在配置文件中删除所有导入别名,改为使用相对路径。运行命令行工具后,你可以恢复使用别名。