function parseOptionalAuth
parseOptionalAuth(ctx: Context): Promise<string | null>

Returns the caller's user id on a route open to anonymous callers, or null when the request has no token. A placeholder such as Bearer undefined counts as no token. An invalid token throws instead of reading as anonymous, so that a stale session gets a 401 the client signs out on, rather than a misleading 404 on a private resource.

Parameters

ctx: Context

Return Type

Promise<string | null>

Throws

APIException

401 when a token is present but invalid.

Usage

import { parseOptionalAuth } from "api/lib/auth.ts";