Loading...
Loading...
Compare original and translation side by side
Is this a new/empty project?
├─ YES → New project setup
│ 1. Identify framework
│ 2. Choose database
│ 3. Install better-auth
│ 4. Create auth.ts + auth-client.ts
│ 5. Set up route handler
│ 6. Run CLI migrate/generate
│ 7. Add features via plugins
│
└─ NO → Does project have existing auth?
├─ YES → Migration/enhancement
│ • Audit current auth for gaps
│ • Plan incremental migration
│ • See migration guides in docs
│
└─ NO → Add auth to existing project
1. Analyze project structure
2. Install better-auth
3. Create auth config
4. Add route handler
5. Run schema migrations
6. Integrate into existing pages这是一个新的/空项目吗?
├─ 是 → 新项目设置
│ 1. 确定框架
│ 2. 选择数据库
│ 3. 安装better-auth
│ 4. 创建auth.ts + auth-client.ts
│ 5. 设置路由处理器
│ 6. 运行CLI migrate/generate命令
│ 7. 通过插件添加功能
│
└─ 否 → 项目已有认证功能?
├─ 是 → 迁移/增强
│ • 审核当前认证方案的不足
│ • 规划增量迁移方案
│ • 查看文档中的迁移指南
│
└─ 否 → 为现有项目添加认证
1. 分析项目结构
2. 安装better-auth
3. 创建认证配置
4. 添加路由处理器
5. 运行 schema 迁移
6. 集成到现有页面中npm install better-auth| Package | Use case |
|---|---|
| WebAuthn/Passkey auth |
| SAML/OIDC enterprise SSO |
| Stripe payments |
| SCIM user provisioning |
| React Native/Expo |
npm install better-auth| 包名 | 使用场景 |
|---|---|
| WebAuthn/Passkey 认证 |
| SAML/OIDC 企业级单点登录 |
| Stripe 支付集成 |
| SCIM 用户配置 |
| React Native/Expo 适配 |
BETTER_AUTH_SECRET=<32+ chars, generate with: openssl rand -base64 32>
BETTER_AUTH_URL=http://localhost:3000
DATABASE_URL=<your database connection string>GITHUB_CLIENT_IDGITHUB_CLIENT_SECRETGOOGLE_CLIENT_IDBETTER_AUTH_SECRET=<32位以上字符,可通过以下命令生成:openssl rand -base64 32>
BETTER_AUTH_URL=http://localhost:3000
DATABASE_URL=<你的数据库连接字符串>GITHUB_CLIENT_IDGITHUB_CLIENT_SECRETGOOGLE_CLIENT_IDlib/auth.tssrc/lib/auth.tsdatabaseemailAndPassword: { enabled: true }socialProvidersemailVerification.sendVerificationEmailemailAndPassword.sendResetPasswordpluginssessionaccount.accountLinkingrateLimitexport type Session = typeof auth.$Infer.Sessionlib/auth.tssrc/lib/auth.tsdatabaseemailAndPassword: { enabled: true }socialProvidersemailVerification.sendVerificationEmailemailAndPassword.sendResetPasswordpluginssessionaccount.accountLinkingrateLimitexport type Session = typeof auth.$Infer.Session| Framework | Import |
|---|---|
| React/Next.js | |
| Vue | |
| Svelte | |
| Solid | |
| Vanilla JS | |
createAuthClient({ plugins: [...] })signInsignUpsignOutuseSessiongetSession| 框架 | 导入路径 |
|---|---|
| React/Next.js | |
| Vue | |
| Svelte | |
| Solid | |
| Vanilla JS | |
createAuthClient({ plugins: [...] })signInsignUpsignOutuseSessiongetSession| Framework | File | Handler |
|---|---|---|
| Next.js App Router | | |
| Next.js Pages | | |
| Express | Any file | |
| SvelteKit | | |
| SolidStart | Route file | |
| Hono | Route file | |
nextCookies()| 框架 | 文件路径 | 处理器 |
|---|---|---|
| Next.js App Router | | |
| Next.js Pages | | |
| Express | 任意文件 | |
| SvelteKit | | |
| SolidStart | 路由文件 | |
| Hono | 路由文件 | |
nextCookies()| Adapter | Command |
|---|---|
| Built-in Kysely | |
| Prisma | |
| Drizzle | |
| 适配器 | 命令 |
|---|---|
| 内置Kysely | |
| Prisma | |
| Drizzle | |
| Database | Setup |
|---|---|
| SQLite | Pass |
| PostgreSQL | Pass |
| MySQL | Pass |
| Prisma | |
| Drizzle | |
| MongoDB | |
| 数据库 | 设置方式 |
|---|---|
| SQLite | 直接传入 |
| PostgreSQL | 直接传入 |
| MySQL | 直接传入 |
| Prisma | 从 |
| Drizzle | 从 |
| MongoDB | 从 |
| Plugin | Server Import | Client Import | Purpose |
|---|---|---|---|
| | | 2FA with TOTP/OTP |
| | | Teams/orgs |
| | | User management |
| | - | API token auth |
| | - | API docs |
| | | WebAuthn |
| | - | Enterprise SSO |
| 插件 | 服务端导入 | 客户端导入 | 用途 |
|---|---|---|---|
| | | 基于TOTP/OTP的双因素认证 |
| | | 团队/组织管理 |
| | | 用户管理 |
| | - | API令牌认证 |
| | - | API文档生成 |
| | | WebAuthn认证 |
| | - | 企业级单点登录 |
signIn.email({ email, password })signIn.social({ provider, callbackURL })erroruseSession(){ data: session, isPending }auth.api.getSession({ headers: await headers() })/sign-insignIn.email({ email, password })signIn.social({ provider, callbackURL })erroruseSession(){ data: session, isPending }auth.api.getSession({ headers: await headers() })/sign-inBETTER_AUTH_SECRETadvanced.useSecureCookies: truetrustedOriginsaccount.accountLinkingBETTER_AUTH_SECRETadvanced.useSecureCookies: truetrustedOriginsaccount.accountLinking| Issue | Fix |
|---|---|
| "Secret not set" | Add |
| "Invalid Origin" | Add domain to |
| Cookies not setting | Check |
| OAuth callback errors | Verify redirect URIs in provider dashboard |
| Type errors after adding plugin | Re-run CLI generate/migrate |