11 lines
467 B
TypeScript
11 lines
467 B
TypeScript
import type { Context } from "@oak/oak";
|
|
import { verifyJWT } from "./jwt.ts";
|
|
|
|
/** Extracts the userId from an optional Bearer token. Returns null if absent or invalid. */
|
|
export async function parseOptionalAuth(ctx: Context): Promise<string | null> {
|
|
const authHeader = ctx.request.headers.get("Authorization");
|
|
if (!authHeader?.startsWith("Bearer ")) return null;
|
|
const payload = await verifyJWT(authHeader.substring(7));
|
|
return payload?.userId ?? null;
|
|
}
|