Add request-scoped context for session.getUser()

Use AsyncLocalStorage to provide request context so services can access
the current user without needing Call passed through every function.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-11 14:56:10 -06:00
parent afcb447b2b
commit 55f5cc699d
3 changed files with 36 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ import { match } from "path-to-regexp";
import { Session } from "./auth";
import { cli } from "./cli";
import { contentTypes } from "./content-types";
import { runWithContext } from "./context";
import { httpCodes } from "./http-codes";
import { routes } from "./routes";
import { services } from "./services";
@@ -75,7 +76,10 @@ routes.forEach((route: Route, _idx: number, _allRoutes: Route[]) => {
};
try {
const retval = await route.handler(req);
const retval = await runWithContext(
{ user: auth.user },
() => route.handler(req),
);
return retval;
} catch (error) {
// Handle authentication errors