Question about actions + cookies

I'm currently trying to port over our company's website from Svelte 5 -> Solid, and I wasn't sure how to replicate this picture in an action. setCookie needs an event to be passed in, and as far as I can tell there's no clear way to get the event. I already tried getRequestEvent and that returned undefined, so I'm a bit stuck with how to do this. Would I have to do this in an API route only? Sveltekit also has those but I didn't have to use one for this, you just get the request and cookies in an action. Thanks.
No description
13 Replies
Madaxen86
Madaxen862w ago
getRequestEvent should work. There’s an example in the docs how to set a cookie https://docs.solidjs.com/reference/server-utilities/get-request-event#response
quinnvaughn
quinnvaughnOP2w ago
it's returning undefined every single time I'm assuming I have to do it inside an action where I use 'use server' but that gives me a different issue where trying to run the action then breaks the browser and I have to just exit the window
peerreynders
peerreynders2w ago
const myAction = action(async (data: FormData) => {
'use server';
const event = getRequestEvent();
// `event` should not be undefined here

// … rest …
}, 'my-action');
const myAction = action(async (data: FormData) => {
'use server';
const event = getRequestEvent();
// `event` should not be undefined here

// … rest …
}, 'my-action');
"use server" So perhaps that other issue needs to be addressed first. Full example: https://github.com/solidjs/solid-start/blob/834f30e5db12d841f18b2c68a92b770dc2619ca4/examples/with-auth/src/lib/index.ts#L27-L47 used here https://github.com/solidjs/solid-start/blob/834f30e5db12d841f18b2c68a92b770dc2619ca4/examples/with-auth/src/routes/login.tsx#L14
GitHub
solid-start/examples/with-auth/src/routes/login.tsx at 834f30e5db12...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
GitHub
solid-start/examples/with-auth/src/lib/index.ts at 834f30e5db12d841...
SolidStart, the Solid app framework. Contribute to solidjs/solid-start development by creating an account on GitHub.
quinnvaughn
quinnvaughnOP2w ago
Yeah I don't know what is obviously wrong about this. But the screen freezes every time I click on the submit button and I have to exit the window.
No description
quinnvaughn
quinnvaughnOP2w ago
Yes I tried both variations - it froze either way
quinnvaughn
quinnvaughnOP2w ago
So this works. So there is something about either the "with"/data typing or the name of the function
No description
peerreynders
peerreynders2w ago
Not necessarily; you haven't put "use server" in yet.
quinnvaughn
quinnvaughnOP2w ago
Yeah shoot never mind - still breaks
peerreynders
peerreynders2w ago
For curiosity; what's the file path?
quinnvaughn
quinnvaughnOP2w ago
No description
peerreynders
peerreynders2w ago
It shouldn't make a difference but have you tried src/routes/login.tsx?
quinnvaughn
quinnvaughnOP2w ago
No difference So it seems to be my middleware is causing the issue That's what seemed to be causing the infinite loop.

Did you find this page helpful?