feat: list + task endpoints and CORS, verified end-to-end

This commit is contained in:
2026-06-10 07:58:51 +00:00
parent 394bceca5f
commit 285bac4308
11 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
// CORS for /api/**. Restricts to the configured web client origin and answers preflight.
// Runs before 1.auth.ts (alphabetical order) so OPTIONS is handled without a token.
export default defineEventHandler((event) => {
if (!getRequestURL(event).pathname.startsWith("/api/")) return;
const origin = useRuntimeConfig().webOrigin;
if (origin) {
setResponseHeader(event, "Access-Control-Allow-Origin", origin);
setResponseHeader(event, "Vary", "Origin");
setResponseHeader(event, "Access-Control-Allow-Headers", "authorization, content-type");
setResponseHeader(event, "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
setResponseHeader(event, "Access-Control-Max-Age", "600");
}
if (event.method === "OPTIONS") {
setResponseStatus(event, 204);
return "";
}
});

View File

@@ -3,6 +3,13 @@ export default defineEventHandler(async (event) => {
const path = getRequestURL(event).pathname;
if (!path.startsWith("/api/")) return;
// Dev-only bypass for local smoke tests. `import.meta.dev` is false in production
// builds, so this branch is dead-code-eliminated and can never run when deployed.
if (import.meta.dev && process.env.AUTH_DEV_BYPASS === "1") {
event.context.user = { sub: "dev" };
return;
}
// CORS preflight is answered (and short-circuited) by 0.cors.ts before this runs.
const header = getHeader(event, "authorization") || "";
const token = header.startsWith("Bearer ") ? header.slice(7).trim() : "";