auth-yes/sdk/hono.ts
google-labs-jules[bot] 9988df3218 audit: verify 3-tier auth, rbac, and decouple server side effects
- Add `app` export and wrap startup logic behind `if (import.meta.main)`
- Extract `hono` middleware into `sdk/hono.ts` for clean separation
- Refactor module imports slightly to support in-memory native mocking (`db`, `valkey`, `spire_ffi`, `ratelimit`, `audit`)
- Implement comprehensive native Deno mock tests in `server/main.test.ts`
- Fix type checking across project files

Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
2026-08-22 00:29:19 +00:00

29 lines
773 B
TypeScript

import type { Context, Next } from "jsr:@hono/hono@4";
import { getCookie } from "jsr:@hono/hono@4/cookie";
import type { AuthSdk } from "./mod.ts";
export function createAuthMiddleware(sdk: AuthSdk) {
return async (c: Context, next: Next) => {
let token = getCookie(c, "session_id");
if (!token) {
const authHeader = c.req.header("Authorization");
if (authHeader && authHeader.startsWith("Bearer ")) {
token = authHeader.substring("Bearer ".length);
}
}
if (!token) {
return c.json({ error: "Unauthorized" }, 401);
}
try {
const userId = await sdk.requireAuth(token);
c.set("userId", userId);
await next();
} catch (_e) {
return c.json({ error: "Unauthorized" }, 401);
}
};
}