@ackplus/nest-auth-客户端
框架不可知 JavaScript/ TypeScript 客户端 @ackplus/nest-auth。零对等依赖性。 现代JS运行的任何地方的作品——浏览器,节点18+,回放土著,Cloudflare工人,德诺,邦.
为什么用它
官方客户端 @ackplus/nest-auth 后端 。 直接从Vue、Angular、Svelte、vanilla JS那里使用,或作为基金会 @ackplus/nest-auth-react。 。 。 .
它处理亲笔写作的活泼部分: 符号持续, 401 上自动刷新并同时请求去除, cookie/header 模式切换, MFA 流量,多租户上下文,以及一个事件订阅 API.
安装
pnpm add @ackplus/nest-auth-client
无对等依存. 软件包中TypeScript定义船.
最小示例
import { AuthClient } from '@ackplus/nest-auth-client';
const auth = new AuthClient({
baseUrl: 'https://api.example.com',
});
await auth.signup({
email: '[email protected]',
password: 'correct horse battery staple',
firstName: 'Alice', // any extra field flows through to UserRegisteredEvent
});
await auth.login({
credentials: { email: '[email protected]', 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 |
配置
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),
});
原生反应
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 | 有TS签名和实例的每一种方法 |
| 配置 | 全体 AuthClientConfig 选项 |
| 存储适配器 | 内部和如何写自己的 |
| HTTP 适应器 | 获取( 默认)、 轴、 自定义 |
| 活动 | 订阅 API |
| 公用事业 | JWT 助手、角色/许可检查 |
配套软件包
| 软件包 | 使用时间 |
|---|---|
@ackplus/nest-auth | NestJS 后端模块 |
@ackplus/nest-auth-react | 反应提供者、钩子、卫士和 Next.js App 路由器帮助器(推荐用于反应) |
@ackplus/nest-auth-contracts | 共享 TS 类型( 已经是此套件的中转分解) |
所有4个包都与同版本号一起放出. 把它们绑在同一个版本.