* Extract the bearer token from the raw `Authorization` header. * * Returns `null` when the header is missing, lacks the case-sensitive * `Bearer ` prefix, or carries an empty token — all treated as 401.
(header: string | undefined)
| 71 | * `Bearer ` prefix, or carries an empty token — all treated as 401. |
| 72 | */ |
| 73 | function extractBearer(header: string | undefined): string | null { |
| 74 | if (header === undefined || !header.startsWith(BEARER_PREFIX)) { |
| 75 | return null; |
| 76 | } |
| 77 | const token = header.slice(BEARER_PREFIX.length); |
| 78 | return token.length === 0 ? null : token; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Build the global `onRequest` auth hook. |