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: 'emailName@email.com', 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).
Was this page helpful?