better-auth-core

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Better 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

快速开始

  1. Install
    better-auth
    .
  2. Set
    BETTER_AUTH_SECRET
    (32+ chars) and
    BETTER_AUTH_URL
    .
  3. Create
    auth.ts
    and export
    auth
    .
  4. Provide
    database
    (driver or adapter) or omit for stateless sessions.
  5. Mount a handler (
    auth.handler
    or a framework helper).
  6. 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,
    },
  },
});
  1. 安装
    better-auth
  2. 设置
    BETTER_AUTH_SECRET
    (32个字符以上)和
    BETTER_AUTH_URL
  3. 创建
    auth.ts
    并导出
    auth
  4. 提供
    database
    (驱动程序或适配器),或者省略以使用无状态会话。
  5. 挂载处理器(
    auth.handler
    或框架助手)。
  6. 使用
    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
    auth
    (or default export) so helpers find it.
  • Keep
    BETTER_AUTH_URL
    in sync with the public base URL.
  • Pass the full base URL to the client if you change the
    /api/auth
    base path.
  • Add database migrations before enabling plugins that require tables.
  • 将实例导出为
    auth
    (或默认导出),以便助手能找到它。
  • 保持
    BETTER_AUTH_URL
    与公共基础URL同步。
  • 如果更改
    /api/auth
    基础路径,请将完整基础URL传递给客户端。
  • 在启用需要数据库表的插件之前,添加数据库迁移。

Server API usage

服务器端API使用

  • Call server endpoints via
    auth.api.*
    with
    { body, headers, query }
    .
  • Use
    asResponse: true
    if you need a
    Response
    object.
  • Use
    returnHeaders: true
    to access
    Set-Cookie
    headers.
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:
    authClient.useSession()
    or
    authClient.getSession()
    .
  • Server:
    auth.api.getSession({ headers })
    .
  • 客户端:
    authClient.useSession()
    authClient.getSession()
  • 服务器端:
    auth.api.getSession({ headers })

TypeScript tips

TypeScript提示

  • Infer types with
    auth.$Infer
    and
    authClient.$Infer
    .
  • Use
    inferAdditionalFields
    on the client when you extend the user schema.
  • 使用
    auth.$Infer
    authClient.$Infer
    推断类型。
  • 当扩展用户模式时,在客户端使用
    inferAdditionalFields

References

参考资料

  • toolchains/platforms/auth/better-auth/better-auth-core/references/setup-database.md
  • toolchains/platforms/auth/better-auth/better-auth-core/references/client-server.md
  • toolchains/platforms/auth/better-auth/better-auth-core/references/typescript.md
  • toolchains/platforms/auth/better-auth/better-auth-core/references/setup-database.md
  • toolchains/platforms/auth/better-auth/better-auth-core/references/client-server.md
  • toolchains/platforms/auth/better-auth/better-auth-core/references/typescript.md