22 lines
567 B
TypeScript
22 lines
567 B
TypeScript
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<TrpcContext> {
|
|
const ctx: TrpcContext = { req: opts.req, resHeaders: opts.resHeaders };
|
|
try {
|
|
ctx.user = await authenticateRequest(opts.req.headers);
|
|
} catch {
|
|
// Authentication is optional here
|
|
}
|
|
return ctx;
|
|
}
|