Integrating Clerk: How to supply req to createTRPCContext on server-side?

Hi! I am currently trying to integrate Clerk into an T3 Setup with tRPC, App Router and Drizzle. Everything goes relatively smooth. Clerk needs the NextRequest object tho, so I updated the createTRPCContext to reflect that:
export const createTRPCContext = async (opts: { req: NextRequest }) => {
return {
db,
auth: getAuth(opts.req),
headers: opts.req.headers,
...opts,
};
};
export const createTRPCContext = async (opts: { req: NextRequest }) => {
return {
db,
auth: getAuth(opts.req),
headers: opts.req.headers,
...opts,
};
};
The problem is on the Server-Side. In server.ts (src/trpc/server.ts) the createTRPCProxyClient creates a trpcLink that uses the following code:
const createContext = cache(() => {
const heads = new Headers(headers());

heads.set("x-trpc-source", "rsc");

return createTRPCContext({
headers: heads,
});
});
const createContext = cache(() => {
const heads = new Headers(headers());

heads.set("x-trpc-source", "rsc");

return createTRPCContext({
headers: heads,
});
});
` I would need to update this, so it supplies the request to createTRPCContext. Does anyone know how to do this? Thanks a lot! Nico
4 Replies
nicolasm8106
nicolasm81066mo ago
Push
Alky
Alky6mo ago
On the app router section of Clerk docs, it doesn't need the Request to be passed in. See here
auth() | Clerk
Access minimal authentication data for managing sessions and data fetching.
ihrishi
ihrishi6mo ago
from what i understand, getAuth() is for pages router. I used auth() and it seems to working fine for me.
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = auth();
return {
db,
session,
...opts,
};
};
export const createTRPCContext = async (opts: { headers: Headers }) => {
const session = auth();
return {
db,
session,
...opts,
};
};
jmerz826
jmerz8265mo ago
Thank you, @ihrishi!
Want results from more Discord servers?
Add your server
More Posts