Reputation: 1801
I try to get cookies of the request from tRPC client using this context.ts of tRPC server:
import { IncomingMessage, ServerResponse } from 'http';
export async function createContext({ req, res }: { req: IncomingMessage; res: ServerResponse }) {
console.log(req.headers);
le user = null;
return {
user,
};
}
export type Context = Awaited<ReturnType<typeof createContext>>;
tRPC server is hosted under http://localhost:2022
and I send requests to it from tRPC client at http://localhost:3000 like
import { createTRPCProxyClient, httpBatchLink } from '@trpc/client';
const trpcClient = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: 'http://localhost:2022',
fetch(url, options) {
return fetch(url, {
...options,
credentials: 'include',
});
},
}),
],
});
export default trpcClient;
and there are cookies with HttpOnly and SameSite=Lax at http://localhost:3000, but they are not sent to tRPC server as I don't see any cookie in headers in console.log(req.headers)
, what I see is only
{
host: 'localhost:2022',
connection: 'keep-alive',
'content-type': 'application/json',
accept: '*/*',
'accept-language': '*',
'sec-fetch-mode': 'cors',
'user-agent': 'node',
'accept-encoding': 'gzip, deflate'
}
I use standalone tRPC server with working cors like
createHTTPServer({
middleware: cors({
origin: 'http://localhost:3000',
credentials: true
}),
router: appRouter,
createContext,
}).listen(2022);
when I go at http://localhost:2022 in browser, I see the cookies
how to receive cookies at the tRPC server?
Upvotes: 1
Views: 139