Files
otunes/packages/convex/client/auth-token.ts
T

16 lines
498 B
TypeScript

/** Reads the user id from a Convex Auth JWT without verifying it, for scoping client caches. */
export function userIdFromToken(token: string | null | undefined) {
const payload = token?.split(".")[1]
if (!payload) {
return null
}
try {
const json = atob(payload.replace(/-/g, "+").replace(/_/g, "/"))
const subject = (JSON.parse(json) as { sub?: unknown }).sub
return typeof subject === "string" ? (subject.split("|")[0] ?? null) : null
} catch {
return null
}
}