better-auth-best-practices

集成 Better Auth(TypeScript 鉴权框架)时使用。支持邮箱密码、OAuth、魔法链接、Passkey 等,通过插件扩展。编写或配置 Better Auth 时触发。

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "better-auth-best-practices" with this command: npx skills add kunhai-88/skills/kunhai-88-skills-better-auth-best-practices

Better Auth 集成指南

代码示例与最新 API 请查阅 better-auth.com/docs

Better Auth 是 TypeScript 优先、框架无关的鉴权框架,通过插件支持邮箱/密码、OAuth、魔法链接、Passkey 等。


速查

环境变量

  • BETTER_AUTH_SECRET:加密密钥(至少 32 字符)。生成:openssl rand -base64 32
  • BETTER_AUTH_URL:根 URL(如 https://example.com

仅当未设置环境变量时,在配置中定义 baseURL / secret

配置文件位置

CLI 在 ././lib./utils./src 下查找 auth.ts。自定义路径用 --config

CLI 命令

  • npx @better-auth/cli@latest migrate — 应用 schema(内置 adapter)
  • npx @better-auth/cli@latest generate — 为 Prisma/Drizzle 生成 schema
  • npx @better-auth/cli mcp --cursor — 为 AI 工具添加 MCP

新增或修改插件后需重新执行。


核心配置项

选项说明
appName可选显示名称
baseURL仅当未设置 BETTER_AUTH_URL
basePath默认 /api/auth,设为 / 可放在根路径
secret仅当未设置 BETTER_AUTH_SECRET
database多数功能必需,见 adapter 文档
secondaryStorageRedis/KV,用于 session 与限流
emailAndPassword{ enabled: true } 启用
socialProviders{ google: { clientId, clientSecret }, ... }
plugins插件数组
trustedOriginsCSRF 白名单

数据库

直连:传入 pg.Poolmysql2 池、better-sqlite3bun:sqlite 实例。

ORM adapter:从 better-auth/adapters/drizzlebetter-auth/adapters/prismabetter-auth/adapters/mongodb 等导入。

注意:Better Auth 使用 adapter 的 model 名,而非表名。若 Prisma model 为 User 对应表 users,用 modelName: "user"(Prisma 引用),而非 "users"


Session 管理

存储优先级

  1. 若配置了 secondaryStorage → session 存此处(不存 DB)
  2. session.storeSessionInDatabase: true 可同时持久化到 DB
  3. 无 DB + cookieCache → 纯无状态模式

Cookie 缓存策略compact(默认)、jwtjwe
关键选项session.expiresInsession.updateAgesession.cookieCache.maxAgesession.cookieCache.version(变更可令所有 session 失效)。


用户与 Account 配置

Useruser.modelNameuser.fieldsuser.additionalFieldsuser.changeEmail.enableduser.deleteUser.enabled
Accountaccount.modelNameaccount.accountLinking.enabledaccount.storeAccountCookie
注册必需字段emailname


邮件流程

  • emailVerification.sendVerificationEmail — 验证邮件发送,必须定义
  • emailVerification.sendOnSignUp / sendOnSignIn — 自动发送时机
  • emailAndPassword.sendResetPassword — 重置密码邮件

安全

advanceduseSecureCookiesdisableCSRFCheck(有风险)、disableOriginCheck(有风险)、crossSubDomainCookies.enabledipAddress.ipAddressHeadersdatabase.generateId 等。
限流rateLimit.enabledrateLimit.windowrateLimit.maxrateLimit.storage


Hooks

端点hooks.before / hooks.after{ matcher, handler } 数组,可用 createAuthMiddleware
数据库databaseHooks.user.create.before/after 等,用于默认值或创建后逻辑。
Hook 上下文sessionsecretadaptergenerateId()baseURL 等。


插件

按路径导入以支持 tree-shaking
import { twoFactor } from "better-auth/plugins/two-factor"
勿从 "better-auth/plugins" 整体导入。

常用插件twoFactororganizationpasskeymagicLinkemailOtpusernameadminapiKeybearerjwtmultiSessionssoopenAPI 等。
客户端插件放在 createAuthClient({ plugins: [...] })


客户端

better-auth/clientbetter-auth/reactbetter-auth/vue 等导入。
常用方法:signUp.email()signIn.email()signIn.social()signOut()useSession()getSession()revokeSession() 等。


类型安全

推断类型:typeof auth.$Infer.Sessiontypeof auth.$Infer.Session.user
前后端分离项目:createAuthClient()


常见坑

  1. Model 与表名 — 配置用 ORM model 名,不是表名
  2. 插件 schema — 增删插件后重跑 CLI
  3. Secondary storage — 默认 session 存此处而非 DB
  4. Cookie cache — 自定义 session 字段不缓存,每次重新拉取
  5. 无状态模式 — 无 DB 时 session 仅存 cookie,缓存过期即登出
  6. 改邮箱流程 — 先发当前邮箱,再发新邮箱

资源

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

programmatic-seo

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

youtube-clipper

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

referral-program

No summary provided by upstream source.

Repository SourceNeeds Review
General

better-auth-best-practices

Better Auth Integration Guide

Repository Source
23.4K156better-auth