{"_id":"@baidu-iot/auth-sdk","_rev":"3-37ec0787616fdd23cd7b682ccfb77cda","name":"@baidu-iot/auth-sdk","dist-tags":{"latest":"0.1.2"},"versions":{"0.1.0":{"name":"@baidu-iot/auth-sdk","version":"0.1.0","license":"UNLICENSED","_id":"@baidu-iot/auth-sdk@0.1.0","maintainers":[{"name":"zhangzhe0804","email":"619961945@qq.com"}],"dist":{"shasum":"2c69c04bda512638b6c01e2d8e76d59017424e21","tarball":"https://registry.npmjs.org/@baidu-iot/auth-sdk/-/auth-sdk-0.1.0.tgz","fileCount":17,"integrity":"sha512-YoRHlodm2TqAHjntNw92wraNeFRKzOkw3H0M3Uy+dowAB7NovbH0Xt1H2ZvUsVw4ou+M4al5aaBNgv7LSKuCzA==","signatures":[{"sig":"MEQCIECfIovoiCVGkWraUMqyzBvntxKdNUp86EZxpMbQbxPfAiAo43xcRbrIX0+UihYaHxGsEerYCBNxBw2T+cpIF4PhSg==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":28994},"main":"dist/index.js","types":"dist/index.d.ts","module":"dist/index.js","gitHead":"76f4d6b88e01569c7e70189719a7399b63e30081","scripts":{"build":"tsc -p tsconfig.build.json","prepublishOnly":"npm run build","publish:public":"npm publish --registry=https://registry.npmjs.org/ --access public"},"_npmUser":{"name":"zhangzhe0804","email":"619961945@qq.com"},"_npmVersion":"10.9.0","description":"通用认证 SDK — token 刷新、session 续期、空闲检测、OAuth URL 构建","directories":{},"sideEffects":false,"_nodeVersion":"23.1.0","_hasShrinkwrap":false,"peerDependencies":{"axios":"^0.19.0"},"peerDependenciesMeta":{"axios":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/auth-sdk_0.1.0_1781611790996_0.9741193754404325","host":"s3://npm-registry-packages-npm-production"}},"0.1.1":{"name":"@baidu-iot/auth-sdk","version":"0.1.1","license":"UNLICENSED","_id":"@baidu-iot/auth-sdk@0.1.1","maintainers":[{"name":"zhangzhe0804","email":"619961945@qq.com"}],"dist":{"shasum":"080efc76c0353b00ba8a3ee4692c8ff57a570bb4","tarball":"https://registry.npmjs.org/@baidu-iot/auth-sdk/-/auth-sdk-0.1.1.tgz","fileCount":18,"integrity":"sha512-NIsrq4GcbujBakkOpBUgXKGLiugM3bnH+RfBGCloLzsiZ49mD+xozKgFxl3/eyHQ7ZssehZwtV1tt5JmLZQcTA==","signatures":[{"sig":"MEQCIDc28YacPhnxlboblHf1jQHLeXld4zloQMlXBngjMw1QAiAw82W+cX5lj/4i/WyYhlpDHF/JX6eanwnWB6jetvfEAA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":32568},"main":"dist/index.js","types":"dist/index.d.ts","module":"dist/index.js","gitHead":"76f4d6b88e01569c7e70189719a7399b63e30081","scripts":{"build":"tsc -p tsconfig.build.json","prepublishOnly":"npm run build","publish:public":"npm publish --registry=https://registry.npmjs.org/ --access public"},"_npmUser":{"name":"zhangzhe0804","email":"619961945@qq.com"},"_npmVersion":"10.9.0","description":"通用认证 SDK — token 刷新、session 续期、空闲检测、OAuth URL 构建","directories":{},"sideEffects":false,"_nodeVersion":"23.1.0","_hasShrinkwrap":false,"peerDependencies":{"axios":"^0.19.0"},"peerDependenciesMeta":{"axios":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/auth-sdk_0.1.1_1781611952072_0.32451254765307413","host":"s3://npm-registry-packages-npm-production"}},"0.1.2":{"name":"@baidu-iot/auth-sdk","version":"0.1.2","description":"通用认证 SDK — token 刷新、session 续期、空闲检测、OAuth URL 构建","main":"dist/index.js","module":"dist/index.js","types":"dist/index.d.ts","sideEffects":false,"scripts":{"build":"tsc -p tsconfig.build.json","prepublishOnly":"npm run build","publish:public":"npm publish --registry=https://registry.npmjs.org/ --access public"},"peerDependencies":{"axios":"^0.19.0"},"peerDependenciesMeta":{"axios":{"optional":true}},"license":"UNLICENSED","gitHead":"be367086f7edcc7457df7f052ad7add49838b336","_id":"@baidu-iot/auth-sdk@0.1.2","_nodeVersion":"23.1.0","_npmVersion":"11.17.0","dist":{"integrity":"sha512-qdUzRKJ8f8Ft7QSBE2Zj62M4xjuCHLBaikpUv4r09mrBz+ge3n2W2V5pEG80GyYVFgpRU4ocHZMh16hA7Gt76A==","shasum":"d1aed4672daceca079de43ff7ce181d87c302b2c","tarball":"https://registry.npmjs.org/@baidu-iot/auth-sdk/-/auth-sdk-0.1.2.tgz","fileCount":18,"unpackedSize":32962,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIBu95pX9djUt8p8huzI6Iy4szBnsD76U77YEDEQWSKUDAiAtnjhDms/BNyXZ7hiMUTeAafxmbYUK83HosruJXossUQ=="}]},"_npmUser":{"name":"zhangzhe0804","email":"619961945@qq.com"},"directories":{},"maintainers":[{"name":"zhangzhe0804","email":"619961945@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/auth-sdk_0.1.2_1781665794817_0.39359801085800217"},"_hasShrinkwrap":false}},"time":{"created":"2026-06-16T12:09:50.871Z","modified":"2026-06-17T03:09:55.084Z","0.1.0":"2026-06-16T12:09:51.130Z","0.1.1":"2026-06-16T12:12:32.231Z","0.1.2":"2026-06-17T03:09:54.963Z"},"license":"UNLICENSED","description":"通用认证 SDK — token 刷新、session 续期、空闲检测、OAuth URL 构建","maintainers":[{"name":"zhangzhe0804","email":"619961945@qq.com"}],"readme":"# @baidu-iot/auth-sdk\n\n通用认证 SDK — 提供 Token 刷新、Session 续期、空闲检测、OAuth URL 构建等认证相关功能。\n\n框架无关，支持任何前端框架（Vue / React / Angular / 原生 JS）。\n\n## 安装\n\n```bash\nnpm install @baidu-iot/auth-sdk\n```\n\n## 快速开始\n\n### Token 刷新（Axios 拦截器）\n\n```ts\nimport axios from 'axios';\nimport {applyAxiosInterceptor} from '@baidu-iot/auth-sdk';\n\nconst api = axios.create({baseURL: '/api'});\n\napplyAxiosInterceptor(api, {\n    refreshTokenCallback: async () => {\n        const resp = await axios.get('/api/token/refresh');\n        return {token: resp.data.data.token};\n    },\n    onAuthFailure: () => {\n        window.location.href = '/login';\n    }\n});\n```\n\n### Session Cookie 续期\n\n```ts\nimport {createCookieRenewer} from '@baidu-iot/auth-sdk';\n\nconst renewer = createCookieRenewer({\n    renewalUrl: '/api/session/renew'\n});\nrenewer.start();\n// 应用销毁时\nrenewer.stop();\n```\n\n### 空闲检测（自动登出）\n\n```ts\nimport {createIdleDetector, clearAuthData} from '@baidu-iot/auth-sdk';\n\nconst detector = createIdleDetector({\n    timeoutMinutes: 15,\n    onTimeout: () => {\n        clearAuthData();\n        window.location.href = '/login';\n    }\n});\ndetector.start();\n```\n\n### OAuth 授权地址构建\n\n```ts\nimport {getOauthUrl} from '@baidu-iot/auth-sdk';\n\nconst url = getOauthUrl({\n    appId: 'your-app-id',\n    clientId: 'your-client-id'\n});\nwindow.location.href = url;\n```\n\n### 底层核心 API\n\n```ts\nimport {createTokenRefresher, isAuthError} from '@baidu-iot/auth-sdk';\n\nconst refresher = createTokenRefresher({\n    refreshTokenCallback: async () => {\n        const resp = await fetch('/api/token/refresh');\n        return resp.json();\n    }\n});\nconst token = await refresher.refreshAndGetToken();\n```\n\n## API 概览\n\n| 导出                      | 分类   | 说明                                     |\n| ------------------------- | ------ | ---------------------------------------- |\n| `applyAxiosInterceptor`   | 适配器 | Axios 响应拦截器，自动刷新 Token         |\n| `createTokenRefresher`    | 核心   | HTTP 客户端无关的 Token 刷新（并发保护） |\n| `isAuthError`             | 核心   | 判断业务错误码是否为鉴权错误             |\n| `createRetryTracker`      | 核心   | 请求重试防无限循环                       |\n| `createCookieRenewer`     | 独立   | Session Cookie 定时续期                  |\n| `createIdleDetector`      | 独立   | 用户空闲检测 / 自动登出                  |\n| `clearAuthData`           | 工具   | 清除 localStorage 中的认证数据           |\n| `createLocalStorageUtils` | 工具   | 创建 localStorage 适配器                 |\n| `getOauthUrl`             | 独立   | 构建 IDaaS OAuth 授权地址                |\n\n## 架构\n\n```\n┌──────────────────────────────────────────────┐\n│              应用层（按需选用）                  │\n│  applyAxiosInterceptor  │\n│  createCookieRenewer    │  createIdleDetector │\n├──────────────────────────────────────────────┤\n│           核心层（与 HTTP 客户端无关）           │\n│  createTokenRefresher   │  isAuthError        │\n│  clearAuthData          │\n└──────────────────────────────────────────────┘\n```\n\n## 类型定义\n\n```ts\ninterface TokenData {\n    token: string;\n    username?: string;\n    role?: string;\n    grantedResource?: any;\n}\n```\n\n完整类型定义见 [dist/types.d.ts](./dist/types.d.ts)。\n\n## 兼容性\n\n-   浏览器环境（Chrome 47+）\n-   无框架依赖\n-   仅 `applyAxiosInterceptor` 需要 `axios`（可选 peer dependency）\n","readmeFilename":"README.md"}