# @connect-xyz/auth-react 中文文档

规范地址：https://npmcn.com/package/%40connect-xyz/auth-react/

版本：0\.84\.13
npm 元数据更新时间：2026-11-04T14:39:34.761Z
中文译文更新时间：2026-11-10T15:32:15.346Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40connect-xyz/auth-react
原文对照：https://npmcn.com/package/%40connect-xyz/auth-react/?lang=original

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：23.4 kB，4 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- peer 依赖：react \>=18\.0\.0，react-dom \>=18\.0\.0
- 首次发布：2025-10-21T19:54:47.965Z
- 最新版本发布：2026-11-04T14:39:34.590Z
- 近 30 天下载量：19909（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：98（近 12 个月 93 个）

## 中文简介

一个能使前端反应应用程序能与Connect Auth产品无缝相接的SDK反应.

## 安装

```sh
npm install @connect-xyz/auth-react
```

也可以使用：`pnpm add @connect-xyz/auth-react`、`yarn add @connect-xyz/auth-react`、`bun add @connect-xyz/auth-react`

## README

# @ connect-xyz/auth- react

一个能使前端反应应用程序能与Connect Auth产品无缝相接的SDK反应.

Connect Auth为加密存款提供了安全,自定义的认证流程. 学习更多在 [连接 Auth 文档](https://docs.zerohash.com/docs/auth)。 。 。 .

## 所需资源

-   反应18.0.0或以上
-   反应DOM 18.0.0或以上

## 安装

```bash
npm install @connect-xyz/auth-react
```

## 开始

遵循这些简单的步骤将连接 Auth 集成到您的 React 应用程序中 :

### 1\. 导入 Auth 组件

```tsx
import { Auth } from '@connect-xyz/auth-react';
```

### 2\. 在您的应用程序中添加 Auth 组件

```tsx
function App() {
  const jwt = 'your-jwt-token'; // Obtain this from your backend authentication

  return (
    <Auth
      jwt={jwt}
      env="production" // or "sandbox" for testing
      theme="auto" // 'auto' (default), 'light', or 'dark'
    />
  );
}
```

### 3\. 配置事件召回(可选)

听从 Auth SDK 处理用户交互的事件 :

```tsx
function App() {
  const jwt = 'your-jwt-token';

  const handleError = ({ errorCode, reason }) => {
    console.log('Auth error occurred:', errorCode, 'Reason:', reason);
  };

  const handleClose = () => {
    console.log('Auth widget closed');
  };

  const handleDeposit = ({ data }) => {
    console.log('Deposit data:', data);
  };

  const handleEvent = ({ type, data }) => {
    console.log('Auth event:', type, 'Data:', data);
  };

  const handleLoaded = () => {
    console.log('Auth widget loaded and ready');
  };

  return (
    <Auth
      jwt={jwt}
      env="production"
      theme="auto" // 'auto' (default), 'light', or 'dark'
      onError={handleError}
      onClose={handleClose}
      onDeposit={handleDeposit}
      onEvent={handleEvent}
      onLoaded={handleLoaded}
    />
  );
}
```

关于召回事件及其有效载荷的详情,见 [Auth SDK 回调文档](https://docs.zerohash.com/docs/auth-standalone#4-organization-frontend-listens-to-auth-sdk-callbacks)。 。 。 .

## 完整示例

以下是将 Connect Auth 整合到您的 React 应用程序的完整实例 :

```tsx
import { Auth } from '@connect-xyz/auth-react';
import React from 'react';

function App() {
  // JWT token should be obtained from your backend
  const jwt = 'your-jwt-token';

  // Choose environment: "production" for live apps, "sandbox" for testing
  const environment = 'production';

  return (
    <div className="App">
      <h1>My Crypto App</h1>

      <Auth
        jwt={jwt}
        env={environment}
        theme="auto" // 'auto' (default), 'light', or 'dark'
        onError={({ errorCode, reason }) => {
          console.log('Error:', errorCode, 'Reason:', reason);
        }}
        onClose={() => {
          console.log('Auth widget closed');
        }}
        onDeposit={({ data }) => {
          console.log('Deposit successful:', data);
        }}
        onEvent={({ type, data }) => {
          console.log('Event type:', type, 'Event data:', data);
        }}
        onLoaded={() => {
          console.log('Auth widget loaded and ready');
        }}
      />
    </div>
  );
}

export default App;
```

## API 参考

### Auth 组件道具

| Prop | 类型 | 需求 | 默认 | 说明 |
| --- | --- | --- | --- | --- |
| `jwt` | `string` | 对 | \- 说吧 | 连接 Auth 认证的 JWT 令牌 |
| `env` | `"production" | "sandbox"` | 没有 | `"production"` | 目标环境 |
| `theme` | `"auto" | "light" | "dark"` | 没有 | `"auto"` | 界面的主题模式 |
| `onError` | `({ errorCode, reason }) => void` | 没有 | \- 说吧 | 调用错误事件 |
| `onClose` | `() => void` | 没有 | \- 说吧 | 部件关闭时召回 |
| `onDeposit` | `({ data }) => void` | 没有 | \- 说吧 | 收回存款 |
| `onEvent` | `({ type, data }) => void` | 没有 | \- 说吧 | 召回一般活动 |
| `onLoaded` | `() => void` | 没有 | \- 说吧 | 部件装入并准备就绪时召回 |

## 更多信息与支持

欲了解有关Connect Auth的全面文献或支持,请访问 [页 次](https://docs.zerohash.com/)。 。 。 .

## 常见问题

### @connect-xyz/auth-react 是什么？

一个能使前端反应应用程序能与Connect Auth产品无缝相接的SDK反应\.

### 如何安装 @connect-xyz/auth-react？

在项目目录运行 npm install @connect-xyz/auth-react，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：react（\>=18\.0\.0）、react-dom（\>=18\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @connect-xyz/auth-react 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @connect-xyz/auth-react 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @connect-xyz/auth-react 可以商用吗？

@connect-xyz/auth-react 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @connect-xyz/auth-react 的下载量有多少？

根据 npm 官方下载统计，@connect-xyz/auth-react 在 2026/10/11 – 2026/11/09 这 30 天内被下载 19,909 次，日均约 664 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @connect-xyz/auth-react 还在维护吗？

最新版本 0\.84\.13 发布于 2026/11/04（6 天前）；至今共发布 98 个版本，其中过去 12 个月发布了 93 个；首次发布于 2025/10/22。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

