Files
admin-web/src/core/auth/jwt.ts
T

29 lines
875 B
TypeScript

import { parseJsonWithSafeIntegers } from '@chongchong/shared'
export interface PlatformJwtClaims {
user_id?: string | number
username?: string
app_code?: string
roles?: string[]
permissions?: string[]
tenant_id?: string | number
membership_id?: string | number
exp?: number
}
function decodeBase64Url(value: string): string {
const normalized = value.replace(/-/g, '+').replace(/_/g, '/')
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, '=')
return atob(padded)
}
export function decodeJwtClaims(token: string): PlatformJwtClaims {
const payload = token.split('.')[1]
if (!payload) throw new Error('登录响应中的 accessToken 格式无效')
try {
return parseJsonWithSafeIntegers(decodeBase64Url(payload)) as PlatformJwtClaims
} catch {
throw new Error('登录响应中的 accessToken 无法解析')
}
}