create-auth-skill
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreate Auth Skill
创建认证技能
Guide for adding authentication to TypeScript/JavaScript applications using Better Auth.
For code examples and syntax, see better-auth.com/docs.
Decision Tree
决策树
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. 集成到现有页面中Installation
安装
Core:
npm install better-authScoped packages (as needed):
| 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 适配 |
Environment Variables
环境变量
env
BETTER_AUTH_SECRET=<32+ chars, generate with: openssl rand -base64 32>
BETTER_AUTH_URL=http://localhost:3000
DATABASE_URL=<your database connection string>Add OAuth secrets as needed: , , , etc.
GITHUB_CLIENT_IDGITHUB_CLIENT_SECRETGOOGLE_CLIENT_IDenv
BETTER_AUTH_SECRET=<32位以上字符,可通过以下命令生成:openssl rand -base64 32>
BETTER_AUTH_URL=http://localhost:3000
DATABASE_URL=<你的数据库连接字符串>按需添加OAuth密钥:、、等。
GITHUB_CLIENT_IDGITHUB_CLIENT_SECRETGOOGLE_CLIENT_IDServer Config (auth.ts)
服务器配置(auth.ts)
Location: or
lib/auth.tssrc/lib/auth.tsMinimal config needs:
- - Connection or adapter
database - - For email/password auth
emailAndPassword: { enabled: true }
Standard config adds:
- - OAuth providers (google, github, etc.)
socialProviders - - Email verification handler
emailVerification.sendVerificationEmail - - Password reset handler
emailAndPassword.sendResetPassword
Full config adds:
- - Array of feature plugins
plugins - - Expiry, cookie cache settings
session - - Multi-provider linking
account.accountLinking - - Rate limiting config
rateLimit
Export types:
export type Session = typeof auth.$Infer.Session存放位置: 或
lib/auth.tssrc/lib/auth.ts最小配置要求:
- - 数据库连接或适配器
database - - 启用邮箱/密码认证
emailAndPassword: { enabled: true }
标准配置新增项:
- - OAuth 提供商(google、github等)
socialProviders - - 邮箱验证处理器
emailVerification.sendVerificationEmail - - 密码重置处理器
emailAndPassword.sendResetPassword
完整配置新增项:
- - 功能插件数组
plugins - - 过期时间、Cookie缓存设置
session - - 多提供商账号关联
account.accountLinking - - 限流配置
rateLimit
导出类型:
export type Session = typeof auth.$Infer.SessionClient Config (auth-client.ts)
客户端配置(auth-client.ts)
Import by framework:
| Framework | Import |
|---|---|
| React/Next.js | |
| Vue | |
| Svelte | |
| Solid | |
| Vanilla JS | |
Client plugins go in .
createAuthClient({ plugins: [...] })Common exports: , , , ,
signInsignUpsignOutuseSessiongetSession按框架导入:
| 框架 | 导入路径 |
|---|---|
| React/Next.js | |
| Vue | |
| Svelte | |
| Solid | |
| Vanilla JS | |
客户端插件需在中配置。
createAuthClient({ plugins: [...] })常用导出: 、、、、
signInsignUpsignOutuseSessiongetSessionRoute Handler Setup
路由处理器设置
| Framework | File | Handler |
|---|---|---|
| Next.js App Router | | |
| Next.js Pages | | |
| Express | Any file | |
| SvelteKit | | |
| SolidStart | Route file | |
| Hono | Route file | |
Next.js Server Components: Add plugin to auth config.
nextCookies()| 框架 | 文件路径 | 处理器 |
|---|---|---|
| Next.js App Router | | |
| Next.js Pages | | |
| Express | 任意文件 | |
| SvelteKit | | |
| SolidStart | 路由文件 | |
| Hono | 路由文件 | |
Next.js Server Components: 在认证配置中添加插件。
nextCookies()Database Migrations
数据库迁移
| Adapter | Command |
|---|---|
| Built-in Kysely | |
| Prisma | |
| Drizzle | |
Re-run after adding plugins.
| 适配器 | 命令 |
|---|---|
| 内置Kysely | |
| Prisma | |
| Drizzle | |
添加插件后需重新运行上述命令。
Database Adapters
数据库适配器
| Database | Setup |
|---|---|
| SQLite | Pass |
| PostgreSQL | Pass |
| MySQL | Pass |
| Prisma | |
| Drizzle | |
| MongoDB | |
| 数据库 | 设置方式 |
|---|---|
| SQLite | 直接传入 |
| PostgreSQL | 直接传入 |
| MySQL | 直接传入 |
| Prisma | 从 |
| Drizzle | 从 |
| MongoDB | 从 |
Common Plugins
常用插件
| Plugin | Server Import | Client Import | Purpose |
|---|---|---|---|
| | | 2FA with TOTP/OTP |
| | | Teams/orgs |
| | | User management |
| | - | API token auth |
| | - | API docs |
| | | WebAuthn |
| | - | Enterprise SSO |
Plugin pattern: Server plugin + client plugin + run migrations.
| 插件 | 服务端导入 | 客户端导入 | 用途 |
|---|---|---|---|
| | | 基于TOTP/OTP的双因素认证 |
| | | 团队/组织管理 |
| | | 用户管理 |
| | - | API令牌认证 |
| | - | API文档生成 |
| | | WebAuthn认证 |
| | - | 企业级单点登录 |
插件使用模式: 服务端插件 + 客户端插件 + 运行迁移。
Auth UI Implementation
认证UI实现
Sign in flow:
- or
signIn.email({ email, password })signIn.social({ provider, callbackURL }) - Handle in response
error - Redirect on success
Session check (client): hook returns
useSession(){ data: session, isPending }Session check (server):
auth.api.getSession({ headers: await headers() })Protected routes: Check session, redirect to if null.
/sign-in登录流程:
- 调用或
signIn.email({ email, password })signIn.social({ provider, callbackURL }) - 处理响应中的
error - 成功后重定向
客户端会话检查: 钩子返回
useSession(){ data: session, isPending }服务端会话检查:
auth.api.getSession({ headers: await headers() })受保护路由: 检查会话,若为空则重定向到。
/sign-inSecurity Checklist
安全检查清单
- set (32+ chars)
BETTER_AUTH_SECRET - in production
advanced.useSecureCookies: true - configured
trustedOrigins - Rate limits enabled
- Email verification enabled
- Password reset implemented
- 2FA for sensitive apps
- CSRF protection NOT disabled
- reviewed
account.accountLinking
- 已设置(32位以上字符)
BETTER_AUTH_SECRET - 生产环境中启用
advanced.useSecureCookies: true - 已配置
trustedOrigins - 已启用限流
- 已启用邮箱验证
- 已实现密码重置功能
- 敏感应用已启用双因素认证
- 未禁用CSRF保护
- 已审核配置
account.accountLinking
Troubleshooting
资源
| 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 |
Resources
—