pixelmund / svelte-kit-cookie-session

⚒️ Encrypted "stateless" cookie sessions for SvelteKit
MIT License
184 stars 12 forks source link

SvelteKit Session Cookie and Houdini #48

Closed yoh-extradat closed 1 year ago

yoh-extradat commented 1 year ago

Setting up a project with SvelteKit Session Cookie and Houdini i ran into some problems and it took me a while to find a working setup.

If is setup +layout.svelte.ts as recommended:

/** @type {import('./$types').LayoutServerLoad} */
export const load = ({ locals }) => {
    return {
        session: locals.session.data // You can also use your old `getSession` function if you wish.
    };
};

The houdini client receives an empty session object. In order to solve this, I have to extent hooks.server.ts

import { handleSession } from 'svelte-kit-cookie-session';
import { sequence } from '@sveltejs/kit/hooks';
import { setSession } from '$houdini'
const sessionHandler = handleSession({
    secret: 'YOUR_SECRET_KEY'
})
export const handle = sequence(sessionHandler, ({ event, resolve }) => {
    setSession(event, event.locals.session.data) //otherwise houdini will not get the session
    return resolve(event)
})

Now queries work, but for mutations the client is still receiving empty session objects. To fix this i have to manually reset houdinisession in +layout.server.ts:

import { GQL_ActiveDonation } from "$houdini";
/** @type {import('./$types').LayoutServerLoad} */
export const load = async (event) => {
    const session = event.locals.session.data || null
        return {
            session: session,
            __houdini__session__: session
        }
    }
};

Any ideas why the recommended implementation isn't working!?

pixelmund commented 1 year ago

Hey, i'm currently trying to create a reproduction but getting blocked by #751.

I'll let you know when i find anything!