Extracted from uncaged monorepo (oc-xiaoju/uncaged). Resolves oc-xiaoju/uncaged#224. - @uncaged/ograph: CF Worker engine (events, projections, reactions) - @uncaged/ograph-cli: CLI for managing OGraph instances - Removed @uncaged/oid dependency (unused) - 116 tests, all passing - CI: GitHub Actions 小橘 🍊(NEKO Team)
22 lines
542 B
TypeScript
22 lines
542 B
TypeScript
import { createMiddleware } from 'hono/factory'
|
|
|
|
type Env = {
|
|
DB: D1Database
|
|
API_TOKEN: string
|
|
}
|
|
|
|
export const bearerAuth = (expectedToken: string) =>
|
|
createMiddleware<{ Bindings: Env }>(async (c, next) => {
|
|
const header = c.req.header('Authorization')
|
|
if (!header?.startsWith('Bearer ')) {
|
|
return c.json({ error: 'Missing or invalid Authorization header' }, 401)
|
|
}
|
|
|
|
const token = header.slice(7)
|
|
if (token !== expectedToken) {
|
|
return c.json({ error: 'Invalid token' }, 401)
|
|
}
|
|
|
|
await next()
|
|
})
|