import type { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"; import type { User } from "@db/schema"; import { authenticateRequest } from "./auth"; export type TrpcContext = { req: Request; resHeaders: Headers; user?: User; }; export async function createContext( opts: FetchCreateContextFnOptions, ): Promise { const ctx: TrpcContext = { req: opts.req, resHeaders: opts.resHeaders }; try { ctx.user = await authenticateRequest(opts.req.headers); } catch { // Authentication is optional here } return ctx; }