better-auth-core
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBetter Auth Core (TypeScript)
Better Auth 核心模块(TypeScript)
Goals
目标
- Set up a Better Auth instance with environment variables and data layer wiring.
- Wire server handlers and a client instance.
- Use sessions and server-side API methods safely.
- Keep data-layer choices pluggable (drivers or adapters).
- 使用环境变量和数据层连接来设置Better Auth实例。
- 连接服务器处理器和客户端实例。
- 安全使用会话和服务器端API方法。
- 保持数据层选项的可插拔性(驱动程序或适配器)。
Quick start
快速开始
- Install .
better-auth - Set (32+ chars) and
BETTER_AUTH_SECRET.BETTER_AUTH_URL - Create and export
auth.ts.auth - Provide (driver or adapter) or omit for stateless sessions.
database - Mount a handler (or a framework helper).
auth.handler - Create a client with .
createAuthClient
ts
import { betterAuth } from "better-auth";
export const auth = betterAuth({
database: myDatabaseOrAdapter, // driver or adapter; omit for stateless mode
emailAndPassword: { enabled: true },
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});- 安装。
better-auth - 设置(32个字符以上)和
BETTER_AUTH_SECRET。BETTER_AUTH_URL - 创建并导出
auth.ts。auth - 提供(驱动程序或适配器),或者省略以使用无状态会话。
database - 挂载处理器(或框架助手)。
auth.handler - 使用创建客户端。
createAuthClient
ts
import { betterAuth } from "better-auth";
export const auth = betterAuth({
database: myDatabaseOrAdapter, // driver or adapter; omit for stateless mode
emailAndPassword: { enabled: true },
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID as string,
clientSecret: process.env.GITHUB_CLIENT_SECRET as string,
},
},
});Core setup checklist
核心设置检查清单
- Export the instance as (or default export) so helpers find it.
auth - Keep in sync with the public base URL.
BETTER_AUTH_URL - Pass the full base URL to the client if you change the base path.
/api/auth - Add database migrations before enabling plugins that require tables.
- 将实例导出为(或默认导出),以便助手能找到它。
auth - 保持与公共基础URL同步。
BETTER_AUTH_URL - 如果更改基础路径,请将完整基础URL传递给客户端。
/api/auth - 在启用需要数据库表的插件之前,添加数据库迁移。
Server API usage
服务器端API使用
- Call server endpoints via with
auth.api.*.{ body, headers, query } - Use if you need a
asResponse: trueobject.Response - Use to access
returnHeaders: trueheaders.Set-Cookie
ts
import { auth } from "./auth";
const session = await auth.api.getSession({
headers: request.headers,
});
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true,
});- 通过调用服务器端点,参数为
auth.api.*。{ body, headers, query } - 如果需要对象,使用
Response。asResponse: true - 使用来访问
returnHeaders: true头。Set-Cookie
ts
import { auth } from "./auth";
const session = await auth.api.getSession({
headers: request.headers,
});
const response = await auth.api.signInEmail({
body: { email, password },
asResponse: true,
});Session access
会话访问
- Client: or
authClient.useSession().authClient.getSession() - Server: .
auth.api.getSession({ headers })
- 客户端:或
authClient.useSession()。authClient.getSession() - 服务器端:。
auth.api.getSession({ headers })
TypeScript tips
TypeScript提示
- Infer types with and
auth.$Infer.authClient.$Infer - Use on the client when you extend the user schema.
inferAdditionalFields
- 使用和
auth.$Infer推断类型。authClient.$Infer - 当扩展用户模式时,在客户端使用。
inferAdditionalFields
References
参考资料
toolchains/platforms/auth/better-auth/better-auth-core/references/setup-database.mdtoolchains/platforms/auth/better-auth/better-auth-core/references/client-server.mdtoolchains/platforms/auth/better-auth/better-auth-core/references/typescript.md
toolchains/platforms/auth/better-auth/better-auth-core/references/setup-database.mdtoolchains/platforms/auth/better-auth/better-auth-core/references/client-server.mdtoolchains/platforms/auth/better-auth/better-auth-core/references/typescript.md