v1.0
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user