Skip to content

createTokenManagerRuntime

桥接 useTokenManagercreateApiClient:注册实例、等待刷新、附加 Bearer、供路由守卫判断登录态。

前置依赖

依赖参数

参数名类型说明
deps.localStoragePick<Storage,'getItem'> | null冷启动回退读 access 物理键
deps.setTimeoutfunction轮询 isRefreshing
deps.Headers构造器附加 Authorization
deps.accessStorageKeystringAUTH_STORAGE_KEY_ACCESS

函数签名

typescript
function createTokenManagerRuntime(
  deps: CreateTokenManagerRuntimeDeps,
): TokenManagerRuntime

返回值

方法说明
registerTokenManager应用启动时注册 useTokenManager 实例
waitForPendingTokenRefreshcreateApiClient 在请求前等待
createAuthRequestInit附加 Bearer
hasStoredAccessToken路由守卫:内存优先,回退 localStorage

应用侧示例

typescript
import { AUTH_STORAGE_KEY_ACCESS } from 'zcw-shared/constants/authStorage'
import { createTokenManagerRuntime } from 'zcw-shared/functions/auth/createTokenManagerRuntime'

export const tokenRuntime = createTokenManagerRuntime({
  localStorage: typeof window !== 'undefined' ? window.localStorage : null,
  setTimeout: globalThis.setTimeout.bind(globalThis),
  Headers,
  accessStorageKey: AUTH_STORAGE_KEY_ACCESS,
})