Setting cookies retrieved from an external api in a form action #8564
-
|
Asked this on Discord as well and figured something out. I have a form action that calls an external API, this external API responds with some data and sets cookies. Base code: export const actions = {
default: async ({ fetch, request, cookies }) => {
const form = await request.formData();
const result = await fetch('...', {
method: 'POST',
body: form,
credentials: "include"
});
cookies.set(/* what goes here ??? */);
return await result.json();
}
}A "solution" I came up with is to(with the use of set-cookie-parser) grab the result.headers.get('set-cookie')?.split(',')
.map(setCookieParser)
.forEach(([c]) => {
cookies.set(c.name, c.value, {
...c
})
})But this somehow feels 'dirty' and leaves me with the strange feeling I am doing something wrong. I also tried using setHeaders like this setHeaders({
"set-cookie": result.headers.get('set-cookie') ?? ''
});but this crashes SvelteKit with a message to use |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
Very good question, unless I'm missing something. Cookie pass-through is fully automatic in the request direction, whereas the response requires manual fiddling as you describe. Is there a better way? |
Beta Was this translation helpful? Give feedback.
-
|
Is there maybe some better approach by now? You can use So I will expect that I can do something like |
Beta Was this translation helpful? Give feedback.
-
|
See #16203 |
Beta Was this translation helpful? Give feedback.
See #16203