# 开始使用 (/docs/infrastructure/getting-started)

本指南将帮助您将 Better Auth Infrastructure 集成到您的应用程序中。



<Steps>
  <Step>
    ## 前提条件 [#前提条件]

    开始之前，请确保您已经拥有：

    1. 一个可用的 [Better Auth](/docs/installation) 安装实例
    2. 来自 [Better Auth 基础设施](/dashboard) 仪表盘的账号和 API 密钥
  </Step>

  <Step>
    ## 安装 [#安装]

    安装 `@better-auth/infra` 包：

    <CodeBlockTabs defaultValue="npm" groupId="persist-install">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="npm">
          npm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="pnpm">
          pnpm
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="yarn">
          yarn
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="bun">
          bun
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="npm">
        ```bash
        npm install @better-auth/infra
        ```
      </CodeBlockTab>

      <CodeBlockTab value="pnpm">
        ```bash
        pnpm add @better-auth/infra
        ```
      </CodeBlockTab>

      <CodeBlockTab value="yarn">
        ```bash
        yarn add @better-auth/infra
        ```
      </CodeBlockTab>

      <CodeBlockTab value="bun">
        ```bash
        bun add @better-auth/infra
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Step>

  <Step>
    ## 环境变量 [#环境变量]

    一旦您从基础设施仪表盘获取到 API 密钥，请在生产环境变量中添加 `BETTER_AUTH_API_KEY` 环境变量。

    ```dotenv
    # 必填：您的 Better Auth 基础设施 API 密钥
    BETTER_AUTH_API_KEY=your_api_key_here
    ```

    <Callout>
      您可以通过注册并在
      [Better Auth 基础设施仪表盘](/dashboard) 创建新项目来获取您的 API 密钥。
    </Callout>
  </Step>
</Steps>

## 服务器设置 [#服务器设置]

### 基础配置 [#基础配置]

将 `dash()` 插件添加到您的 Better Auth 配置中：

```ts
import { betterAuth } from "better-auth";
import { dash } from "@better-auth/infra"; // [!code highlight]

export const auth = betterAuth({
  // ... 您现有的 Better Auth 配置
  plugins: [
    dash({ // [!code highlight]
      apiKey: process.env.BETTER_AUTH_API_KEY, // [!code highlight]
    }), // [!code highlight]
  ],
});
```

如果您订阅的是 **专业版** 计划及以上，您可以使用 `sentinel()` 插件启用安全检查、滥用防护等功能：

```ts
import { betterAuth } from "better-auth";
import { dash, sentinel } from "@better-auth/infra";  // [!code highlight]

export const auth = betterAuth({
  plugins: [
    dash({ /* ... */ }),
    sentinel({ // [!code highlight]
      apiKey: process.env.BETTER_AUTH_API_KEY, // [!code highlight]
    }) // [!code highlight]
  ],
});
```

## 客户端设置 [#客户端设置]

### 基础客户端配置 [#基础客户端配置]

将客户端插件添加到您的认证客户端中：

```ts
import { createAuthClient } from "better-auth/client";
import { dashClient, sentinelClient } from "@better-auth/infra/client";

export const authClient = createAuthClient({
  plugins: [
    dashClient(),
    sentinelClient({
      autoSolveChallenge: true, // 自动解决 PoW 挑战
    }),
  ],
});
```

### Expo 和 React Native [#expo-和-react-native]

对于 **Expo** 或 **React Native** 客户端，请从 `@better-auth/infra/native` 导入，而不是 `@better-auth/infra/client`。原生入口提供 `dashClient`（与 Web 客户端相同的审计日志 API）和 `sentinelNativeClient`。

```ts
import { createAuthClient } from "better-auth/client";
import { dashClient, sentinelNativeClient } from "@better-auth/infra/native";

export const authClient = createAuthClient({
  baseURL: "https://your-api.example.com",
  plugins: [
    dashClient(),
    sentinelNativeClient({
      autoSolveChallenge: true,
    }),
  ],
});
```

详见 [Sentinel — Expo 和 React Native](/docs/infrastructure/plugins/sentinel#expo-和-react-native) 了解更多详情。

### 插件概览 [#插件概览]

* **`dash()`** - 启用分析跟踪、审计日志、仪表盘管理 API 等功能
* **`sentinel()`** - 启用安全检查和滥用防护

您可以单独使用其中任何一个插件，但同时使用两者可获得完整的 Better Auth 基础设施体验。

## 后续步骤 [#后续步骤]

既然您已经完成基础设置，可继续探索以下内容：

* [仪表盘](/docs/infrastructure/plugins/dashboard) - 分析、活动跟踪和管理 API
* [审计日志](/docs/infrastructure/plugins/audit-logs) - 跟踪和查询认证事件
* [Sentinel](/docs/infrastructure/plugins/sentinel) - 配置高级安全
* [邮件服务](/docs/infrastructure/services/email) - 设置事务性邮件
* [短信服务](/docs/infrastructure/services/sms) - 启用短信验证

