S
SolidJS8mo ago
dion

Request data not consistent between createServerData$ and API route

I have a lucia x trpc x solidstart project setup. TLDR: I'm observing that the event passed to my createServerData$ and the event that is passed to my API handler is different. Here's my project structure:
src
|--auth
|--routes
| |--(auth)
| | |--login.tsx <-- this one has access to the right cookie
| |-- api
| | |--trpc
| | | |--[trpc].ts <-- this one does not have access to the cookie even though login.tsx already has
|--api
src
|--auth
|--routes
| |--(auth)
| | |--login.tsx <-- this one has access to the right cookie
| |-- api
| | |--trpc
| | | |--[trpc].ts <-- this one does not have access to the cookie even though login.tsx already has
|--api
Here's how my API handler for trpc has been setup src/routes/trpc/[trpc].ts
const handler = async ({ request, params }: APIEvent) =>
{
const cookie = parseCookie(request.headers.get("Cookie") ?? "");
console.log(cookie); <-- this logs as {}
};

export const GET = handler;
export const POST = handler;
const handler = async ({ request, params }: APIEvent) =>
{
const cookie = parseCookie(request.headers.get("Cookie") ?? "");
console.log(cookie); <-- this logs as {}
};

export const GET = handler;
export const POST = handler;
The cookie here is logged as {} On the other hand, in another route, src/routes/(auth)/signin.ts
export const routeData = () => {
return createServerData$(async (_, event) => {
const authRequest = auth.handleRequest(event.request);
const session = await authRequest.validate();
const cookie = parseCookie(event.request.headers.get("Cookie") ?? "");
console.log(cookie); <-- This logs out the auth details
});
};

const Page = () => {
return (
<div class="w-full h-screen items-center flex justify-center">
...
</div>
);
};

export default Page;
export const routeData = () => {
return createServerData$(async (_, event) => {
const authRequest = auth.handleRequest(event.request);
const session = await authRequest.validate();
const cookie = parseCookie(event.request.headers.get("Cookie") ?? "");
console.log(cookie); <-- This logs out the auth details
});
};

const Page = () => {
return (
<div class="w-full h-screen items-center flex justify-center">
...
</div>
);
};

export default Page;
The console logs out the authentication details. I'm confused as to why this is the behaviour since I thought both routeData and API routes are accessing the same request
1 Reply
dion
dion8mo ago
Okay, it seems that the reason for this was because the client headers were not being forwarded over to the trpc endpoint if im understanding right. If that's the case, is there a way to forward headers in my context?