Session exists in server component, not in trpc context.

Inside of a server component I call: const session = await getServerAuthSession(); console.log("Check session from Server component: ", session); This prints when im Logged in: Check session from Server component: { user: { name: null, email: '[email protected]', image: null, id: 'userIdBlahBlah' } } Chill all good. Now I want to call a TRPC protected procedure after that session check: const session = await getServerAuthSession(); const verificationResult = await api.user.verifyLoggedInUser.mutate(); .... and I get an UNAUTHORIZED Error. ....as it should for protected procedures....weird because I just logged the session and its valid....then I check the handler creation: const handler = (req: NextRequest) => { console.log("Check session from trpc middleware: ", req); return fetchRequestHandler({ endpoint: "/api/trpc", req, router: appRouter, createContext: () => createTRPCContext({ req }), onError: env.NODE_ENV === "development" ? ({ path, error }) => { console.error( ❌ tRPC failed on ${path ?? "<no-path>"}: ${error.message}, ); } : undefined, }); }; export { handler as GET, handler as POST }; The cookies of next-auth are there no problem. But the middleware thinks the server Auth Session is null: export const createInnerTRPCContext = async (opts: CreateContextOptions) => { const session = await getServerAuthSession(); console.log("Check session from trpc middleware creation: ", session); return { session, headers: opts.headers, db, }; }; Session is null!! I think this is a bug, because if I call getServerAuthSession in the component and I get a session, but I dont get the same result in the trpc handler, is incosistent. Or am I missing something ? Thanks in advance! (I am using the experimental appdir with drizzle).
2 Replies
cje
cje14mo ago
i just scaffolded a fresh app, appdir and all packages, turned hello into a protectedProcedure and logged ctx.session there, as well as in createInnerContext. it exists in both places. so most likely it's something related to your application code. can't help further without a minimal reproduction repo
nvegater
nvegaterOP14mo ago
oh no :/, I will try to compare the code in more detail. I found the issue. The initial setup of t3 scaffold from the time I started the project (23/10/2023) for the api Proxy client was: headers() { const heads = new Map(headers()); heads.set("x-trpc-source", "rsc"); return Object.fromEntries(heads); }, Now is: headers() { return { cookie: cookies().toString(), "x-trpc-source": "rsc", }; },
Want results from more Discord servers?
Add your server