feat: 前端界面
This commit is contained in:
@@ -13,12 +13,19 @@ function base64UrlToBase64(input: string) {
|
||||
return pad === 0 ? base64 : base64 + '='.repeat(4 - pad)
|
||||
}
|
||||
|
||||
function base64ToUtf8String(base64: string) {
|
||||
const binary = atob(base64)
|
||||
const bytes = new Uint8Array(binary.length)
|
||||
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i)
|
||||
return new TextDecoder().decode(bytes)
|
||||
}
|
||||
|
||||
export function decodeJwtPayload(token: string): JwtPayload | null {
|
||||
const [, payload] = token.split('.')
|
||||
if (!payload) return null
|
||||
|
||||
try {
|
||||
const json = atob(base64UrlToBase64(payload))
|
||||
const json = base64ToUtf8String(base64UrlToBase64(payload))
|
||||
const parsed = JSON.parse(json)
|
||||
if (!parsed || typeof parsed !== 'object') return null
|
||||
return parsed as JwtPayload
|
||||
|
||||
Reference in New Issue
Block a user