项目文件夹

文件
wehub-resource-sync 98e40dac97
CLI Smoke Test / smoke-test-linux (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-linux (24) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (20) (push) Has been cancelled
CLI Smoke Test / smoke-test-windows (24) (push) Has been cancelled
Expo App TypeScript typecheck / typecheck (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:40:49 +08:00

23 行
959 B
TypeScript

import { decodeBase64 } from "@/encryption/base64";
import { decodeUTF8, encodeUTF8 } from "@/encryption/text";
export function parseToken(token: string) {
const parts = token.split('.');
if (parts.length !== 3 || !parts[0] || !parts[1] || !parts[2]) {
throw new Error('Invalid token format: expected "header.payload.signature" with non-empty parts');
}
const [header, payload, signature] = parts;
try {
const sub = JSON.parse(decodeUTF8(decodeBase64(payload))).sub;
if (typeof sub !== 'string') {
throw new Error('Invalid token: missing or invalid sub claim');
}
return sub;
} catch (error) {
if (error instanceof Error && error.message.includes('Invalid token')) {
throw error; // Re-throw our validation errors
}
throw new Error(`Invalid token: failed to decode payload - ${error instanceof Error ? error.message : 'unknown error'}`);
}
}