Svelte Kit hooks.server.ts getSession not working

I want to use better-auth with SvelteKit, but I don't use the svelteKitHandler.. I've set-up a custom route apps/frontend/src/routes/api/auth/[...all]/+server.ts with the following code:
import type { RequestHandler } from "./$types";

// const ORPC_ENDPOINT = import.meta.env.VITE_ORPC_ENDPOINT

export const fallback: RequestHandler = async ({ request, platform }) => {
console.log("Fallback handler called for:", request.url);
// if (import.meta.env.DEV) {
// return await fetch(`${ORPC_ENDPOINT}${new URL(request.url).pathname}`, request)
// } else {
const t = await platform!.env.ORM.fetch(`http://localhost:8210${new URL(request.url).pathname}`, request)

return new Response(t.body, {
status: t.status,
statusText: t.statusText,
headers: t.headers,
});
};
import type { RequestHandler } from "./$types";

// const ORPC_ENDPOINT = import.meta.env.VITE_ORPC_ENDPOINT

export const fallback: RequestHandler = async ({ request, platform }) => {
console.log("Fallback handler called for:", request.url);
// if (import.meta.env.DEV) {
// return await fetch(`${ORPC_ENDPOINT}${new URL(request.url).pathname}`, request)
// } else {
const t = await platform!.env.ORM.fetch(`http://localhost:8210${new URL(request.url).pathname}`, request)

return new Response(t.body, {
status: t.status,
statusText: t.statusText,
headers: t.headers,
});
};
This is because it uses two Cloudflare Workers, one running the server and the other just a proxy. Now i want to use inside hooks.server.ts the following code
const addGraphQLClient: Handle = async ({ event, resolve }) => {
const { data: session } = await authClient.getSession({
query: {
disableCookieCache: true,
disableRefresh: false
},
fetchOptions: {
headers: event.request.headers
}
});
}
const addGraphQLClient: Handle = async ({ event, resolve }) => {
const { data: session } = await authClient.getSession({
query: {
disableCookieCache: true,
disableRefresh: false
},
fetchOptions: {
headers: event.request.headers
}
});
}
The cookie inside event.request.headers does include the etter-auth.session_token=, but it still always return null.. I can't use auth.api.getSession because the auth instance runs in a different worker, but according to the docs it should work with https://www.better-auth.com/docs/reference/faq I think I'm maybe missing some setup, which is needed if I use a custom api route and note the svelteKitHandler, but I'm a bit lost. Can anyone help?
2 Replies
alex (he/him)
alex (he/him)OP•2w ago
Calling the proxy route with fetch directly, works and returns the session 🤔 I'm lost why it doesn't work with the authClient
const data = await fetch('http://localhost:8210/api/auth/get-session', {
headers: {
cookie: event.request.headers.get('cookie') || '',
},
}).then(res => res.json());
const data = await fetch('http://localhost:8210/api/auth/get-session', {
headers: {
cookie: event.request.headers.get('cookie') || '',
},
}).then(res => res.json());
Jexxxysun
Jexxxysun•3d ago
import { authClient } from '$lib/auth-client';
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {

const data = await authClient.getSession({
fetchOptions: {
headers: {
cookie: event.request.headers.get('cookie') || '',
},
}
});

console.log(data);


if (event.url.pathname.startsWith('/custom')) {
return new Response('custom response');
}

const response = await resolve(event);
return response;
};
import { authClient } from '$lib/auth-client';
import type { Handle } from '@sveltejs/kit';

export const handle: Handle = async ({ event, resolve }) => {

const data = await authClient.getSession({
fetchOptions: {
headers: {
cookie: event.request.headers.get('cookie') || '',
},
}
});

console.log(data);


if (event.url.pathname.startsWith('/custom')) {
return new Response('custom response');
}

const response = await resolve(event);
return response;
};
This will work fine

Did you find this page helpful?