40d3a66055
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
32 lines
913 B
TypeScript
32 lines
913 B
TypeScript
import type { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
|
|
import type { User } from "@db/schema";
|
|
import * as cookie from "cookie";
|
|
import { Session } from "@contracts/constants";
|
|
import { verifySessionToken } from "./lib/session";
|
|
import { findUserById } from "./queries/users";
|
|
|
|
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 {
|
|
const cookies = cookie.parse(opts.req.headers.get("cookie") || "");
|
|
const token = cookies[Session.cookieName];
|
|
if (token) {
|
|
const claim = await verifySessionToken(token);
|
|
if (claim) {
|
|
ctx.user = await findUserById(claim.userId);
|
|
}
|
|
}
|
|
} catch {
|
|
// Authentication is optional
|
|
}
|
|
return ctx;
|
|
}
|