# @ackplus/nest-auth-client 中文文档

规范地址：https://npmcn.com/package/%40ackplus/nest-auth-client/

版本：2\.18\.0
npm 元数据更新时间：2026-11-03T08:06:05.615Z
中文译文更新时间：2026-11-06T20:17:23.114Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40ackplus/nest-auth-client
原文对照：https://npmcn.com/package/%40ackplus/nest-auth-client/?lang=original
项目主页：https://github.com/ack-solutions/nest-auth#readme
源码仓库：https://github.com/ack-solutions/nest-auth

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

- 包体积（解压后）：231 kB，78 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2025-12-27T04:39:26.069Z
- 最新版本发布：2026-11-03T08:06:05.434Z
- 近 30 天下载量：2384（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：110（近 12 个月 110 个）

## 中文简介

REST API 的 NestJS 认证

## 安装

```sh
npm install @ackplus/nest-auth-client
```

也可以使用：`pnpm add @ackplus/nest-auth-client`、`yarn add @ackplus/nest-auth-client`、`bun add @ackplus/nest-auth-client`

## README

# @ackplus/nest-auth-客户端

[![npm version](https://img.shields.io/npm/v/@ackplus/nest-auth-client.svg)](https://www.npmjs.com/package/@ackplus/nest-auth-client) [![npm downloads](https://img.shields.io/npm/dm/@ackplus/nest-auth-client.svg)](https://www.npmjs.com/package/@ackplus/nest-auth-client) [![license](https://img.shields.io/npm/l/@ackplus/nest-auth-client.svg)](https://www.npmjs.com/package/@ackplus/nest-auth-client)

框架不可知 JavaScript/ TypeScript 客户端 [`@ackplus/nest-auth`](https://www.npmjs.com/package/@ackplus/nest-auth)。零对等依赖性。 现代JS运行的任何地方的作品——浏览器,节点18+,回放土著,Cloudflare工人,德诺,邦.

> 📚 **完整文档: [ack-solutions.github.io/nst-auth/docs/客户](https://ack-solutions.github.io/nest-auth/docs/client/)**

* * *

## 为什么用它

官方客户端 `@ackplus/nest-auth` 后端 。 直接从Vue、Angular、Svelte、vanilla JS那里使用,或作为基金会 [`@ackplus/nest-auth-react`](https://www.npmjs.com/package/@ackplus/nest-auth-react)。 。 。 .

它处理亲笔写作的活泼部分: 符号持续, 401 上自动刷新并同时请求去除, cookie/header 模式切换, MFA 流量,多租户上下文,以及一个事件订阅 API.

## 安装

```bash
pnpm add @ackplus/nest-auth-client
```

**无对等依存.** 软件包中TypeScript定义船.

## 最小示例

```ts
import { AuthClient } from '@ackplus/nest-auth-client';

const auth = new AuthClient({
  baseUrl: 'https://api.example.com',
});

await auth.signup({
  email: 'alice@example.com',
  password: 'correct horse battery staple',
  firstName: 'Alice',     // any extra field flows through to UserRegisteredEvent
});

await auth.login({
  credentials: { email: 'alice@example.com', password: 'correct horse battery staple' },
});

if (auth.getIsAuthenticated()) {
  const user = await auth.getSessionUserData();
  console.log('Hello', user.email);
}
```

## 包括什么

| 能力 | 页:1 |
| --- | --- |
| **自动流量** | `signup`, (中文(简体) ), `login`, (中文(简体) ), `logout`, (中文(简体) ), `logoutAll`, (中文(简体) ), `refresh`, (中文(简体) ), `passwordlessSend`, (中文(简体) ), `switchTenant` |
| **外交部** | `send2fa`, (中文(简体) ), `verify2fa`, (中文(简体) ), `setupTotp`, (中文(简体) ), `verifyTotpSetup`, (中文(简体) ), `getMfaStatus`,回收代码 |
| **密码管理** | `forgotPassword`, (中文(简体) ), `verifyForgotPasswordOtp`, (中文(简体) ), `resetPassword`, (中文(简体) ), `changePassword` |
| **电子邮件/电话核查** | `sendEmailVerification`, (中文(简体) ), `verifyEmail`, (中文(简体) ), `sendPhoneVerification`, (中文(简体) ), `verifyPhone` |
| **自动更新** | 401 — 刷新 — 重试一次, 用 `RefreshQueue` 分解后, N 平行 401s 只发火 **一个** 刷新 |
| **页眉或饼干模式** | `accessTokenType: 'header' | 'cookie' | null` (自动检测) |
| **存储适配器** | `MemoryStorage`, (中文(简体) ), `LocalStorageAdapter`, (中文(简体) ), `SessionStorageAdapter`, (中文(简体) ), `CookieStorageAdapter`,自定义 |
| **HTTP 适配器** | `FetchAdapter` (违约)或 `createAxiosAdapter(yourAxiosInstance)`,自定义 |
| **活动** | `onTokensSet`, (中文(简体) ), `onTokenRefreshed`, (中文(简体) ), `onTokensRemoved`, (中文(简体) ), `onLogout`, (中文(简体) ), `onError`, (中文(简体) ), `onSessionVerified` |
| **公用事业** | `decodeJwt`, (中文(简体) ), `isTokenExpired`, (中文(简体) ), `hasRole`, (中文(简体) ), `hasPermission`, (中文(简体) ), `hasAnyAccess`, (中文(简体) ), `hasAllAccess` |

## 配置

```ts
new AuthClient({
  baseUrl: 'https://api.example.com',
  storage: new LocalStorageAdapter('myapp_'),
  httpAdapter: new FetchAdapter(),
  accessTokenType: 'header',     // or 'cookie' or null (auto-detect)
  autoRefresh: true,
  refreshThreshold: 60,          // seconds before expiry
  onTokenRefreshed: (tokens) => { /* … */ },
  onLogout: () => { window.location.href = '/login'; },
  onError: (err) => console.error(err),
});
```

[完全配置参考 {}](https://ack-solutions.github.io/nest-auth/docs/client/config/)

## 原生反应

```ts
import AsyncStorage from '@react-native-async-storage/async-storage';
import { AuthClient, type StorageAdapter } from '@ackplus/nest-auth-client';

const RNStorage: StorageAdapter = {
  get:    (k) => AsyncStorage.getItem(k),
  set:    (k, v) => AsyncStorage.setItem(k, v),
  remove: (k) => AsyncStorage.removeItem(k),
  clear:  () => AsyncStorage.clear(),
};

export const auth = new AuthClient({
  baseUrl: 'https://api.example.com',
  storage: RNStorage,
});
```

## 文档

| 节次 | 这是什么 |
| --- | --- |
| [`AuthClient`](https://ack-solutions.github.io/nest-auth/docs/client/client/) | 有TS签名和实例的每一种方法 |
| [配置](https://ack-solutions.github.io/nest-auth/docs/client/config/) | 全体 `AuthClientConfig` 选项 |
| [存储适配器](https://ack-solutions.github.io/nest-auth/docs/client/storage-adapters/) | 内部和如何写自己的 |
| [HTTP 适应器](https://ack-solutions.github.io/nest-auth/docs/client/http-adapters/) | 获取( 默认)、 轴、 自定义 |
| [活动](https://ack-solutions.github.io/nest-auth/docs/client/events/) | 订阅 API |
| [公用事业](https://ack-solutions.github.io/nest-auth/docs/client/utilities/) | JWT 助手、角色/许可检查 |

## 配套软件包

| 软件包 | 使用时间 |
| --- | --- |
| [`@ackplus/nest-auth`](https://www.npmjs.com/package/@ackplus/nest-auth) | NestJS 后端模块 |
| [`@ackplus/nest-auth-react`](https://www.npmjs.com/package/@ackplus/nest-auth-react) | 反应提供者、钩子、卫士和 Next.js App 路由器帮助器(推荐用于反应) |
| [`@ackplus/nest-auth-contracts`](https://www.npmjs.com/package/@ackplus/nest-auth-contracts) | 共享 TS 类型( 已经是此套件的中转分解) |

所有4个包都与同版本号一起放出. 把它们绑在同一个版本.

## 链接

-   📚 [文档](https://ack-solutions.github.io/nest-auth/)
-   💬 [问题跟踪器](https://github.com/ack-solutions/nest-auth/issues)
-   📦 [GitHub 仓库](https://github.com/ack-solutions/nest-auth)

## 许可证

[麻省理工学院](https://github.com/ack-solutions/nest-auth/blob/main/LICENSE)

## 常见问题

### @ackplus/nest-auth-client 是什么？

REST API 的 NestJS 认证

### 如何安装 @ackplus/nest-auth-client？

在项目目录运行 npm install @ackplus/nest-auth-client，或使用 pnpm add / yarn add / bun add。

### @ackplus/nest-auth-client 支持 TypeScript 吗？

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

### @ackplus/nest-auth-client 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @ackplus/nest-auth-client 可以商用吗？

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

### @ackplus/nest-auth-client 的下载量有多少？

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

### @ackplus/nest-auth-client 还在维护吗？

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

