-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Server-side fetch
in load
is not credentialed
#672
Comments
It's apparently a five-month old comment, but probably! kit/packages/app-utils/src/render/page.ts Line 104 in 4267d8e
|
this is the issue in node-red for that node-fetch/node-fetch#1113 |
in order to move forward here |
i was able to make it work by adding this code here after line 90
You also need to add
at the beginning then rebuild sveltekit via it works now and my server side rendering is actually sending the cookie credentials to the backend. However i am hesitant to open a Pull Request because i feel like i am not understanding 100% of the puzzle and don't want to make any troubles (e.g. sapper is actually merging the request & response cookies (by looking at the SET-Cookie header from the response) but i don't know how this can be done in sveltekit) |
@eikaramba This is a good workaround for setting headers with ssr, but it only worked for me when placed above that conditional. |
yeah it could be that it actually needs to be executed for internal and external calls. i am calling a backend api on localhost from svelte, not first fetching some internal svelte ressource, which then calls my backend api. |
Closed via #835 |
Reopening as this is not quite fixed. Internal fetches like "External" fetches like await fetch('http://localhost:4000/test', {
credentials: 'include',
mode: 'cors'
}); Probably because we don't pass it to the external node fetch: kit/packages/kit/src/runtime/server/page.js Lines 82 to 85 in 108c26c
|
We need to be really careful about what's 'same origin' because the server has no idea what host/path the various cookies are associated with. It just has a list of cookies that the browser had determined to be relevant for this SSR'd page, and not for any other subrequests. |
Right, I think forcing the user to be explicit about wanting to do a "cross-origin" fetch makes sense. Should |
What I meant is that all the server will have are the wrong cookies. It's not like on the browser where we can jump through CORS hoops to make a page on domain1 talk to domain2 using domain2's cookies. On the server, we'd have domain1 talking to domain2 with domain1's cookies. |
Is there an actual solution to this problem? I was hoping for this to eventually work as it would significantly simplify the authentication in my application so this is kind of a blocker for me. |
Due to the origin issues Conduitry mentioned there's not really a way to do cross-origin requests. If you do need to passthrough your For same-origin I'm not sure how we can reasonably detect |
I don't really want to re-implement all of my api endpoints to make this work. Is there a way to "mask" the thirdparty endpoints to pass through the cookies? |
This is a blocker for me as well, as I don't really understand how to interact with my backend while it's not implemented within sveltekit and uses cookie-based authentication. I don't know how much workaround is it, but for now I'm using this approach: myproject/src/routes/api/[...route].ts:
This way I'm able to pass cookie back and forth, so this code kind of works:
Doesn't seem to me as an optimal approach, because in involves some manual parsing and non-obvious assumptions. |
The problem is that the browser will not send first-party If you need If you control both domains, maybe you can make the cookie's |
This feels like a common use case for SvelteKit. I want to write my frontend in SvelteKit and have a separate api. What would be the optimal way to do this? Using token-based auth? Something else? |
My typical sapper workflow had a mix of API endpoints (in express). This was like nirvana to me- i miss it in SK. I also think that having the 'escape hatch' of expresses middleware ecosystem was one of the biggest selling points of sapper. I'd love to be able to do the same thing. |
The original bug has been resolved. There's not a way to know about what cookies exist on other domains during SSR. Supporting the Express middleware API was discussed internally and decided against. If they want, someone could write a userland interop module between the Express API and the |
in how far was the original issue resolved? I use cookie based authentication. Backend Cookie set by:
Svelte-kit Hook
if i use my graphql lib AFTER intital page load it works (no SSR), however if it is called form svelte server it does not work as authentication cookie is not retrieved. The workaround code change which i wrote at the very beginning of this issue fixes it (not saying that it should be incorporated due to the issues in this discussion in regards to 3rd party cookies) |
@schibrikov 's solution almost works for us, but unfortunately Apollo requires you to have a full URI for their config. The problem with that is that Svelte Kit, even with the proxied API route is seeing that URI as a different origin and still doesn't pass in our Headers. So while hitting that proxied url with a fetch using a relative path work, the same relative path doesn't play nice with Apollo client. Wondering if you found any solutions @eikaramba |
i ditched apollo, because it just does not work for me with svelteKit. in dev mode yes, but as soon as i started building it broke everything. now i am just using a simple plain fetch to the graphql endpoint. that said, the issue from this ticket is still a huge problem. the workaround from @schibrikov is kind of working although i have a problem that the endpoint doesn't seem to accept post requests when using the rest parameter. it always fire the exported GET method even if the svelte component uses fetch with method:"POST". i guess i will settle with this workaround but it really feels quite weird to first go over the internal node server to finally access my own backend if i want to use authentication (which of course i want). |
I'd just like to add that I am also being frustrated by this bug. A Graphql server on localhost:8080 and site on localhost:3000 and they don't play nicely. In production I'd like to have the api on a subdomain. These are pretty normal setups and should not be so difficult to get working. I can't understand why this issue is closed. |
@daamsie Part of the original issue has been solved. The other part of the issue is inherently problematic because cookies cannot cross origins. The issue is not closed as a "won't fix", it's closed because SvelteKit cannot change the behavior of the browser. To solve the problem in a secure manner, the recommended setup is to proxy your API:
Using, for example, |
My understanding here is that we're experiencing this in situations where we aren't attempting to cross origin. Ie subdomain or localhost. Where some libraries like apollo require a full url and not a relative one. |
My problems are for situations where the domain is the same though. A cookie can be shared across subdomains or ports. That is not a browser limitation. Edit: and yes, I'm also trying to use Apollo so need to use the full URL |
I think this is beyond the scope of the original issue so I'll file a new one specifically for same-origin "external" fetches. |
Awesome,thank you. I think determining that something is an external fetch call based on relative or absolute url might throw a lot of people off. |
The new issue will be tracked at #1777. |
Describe the bug
No credentials are passed on in a server-side
fetch
.To Reproduce
On a fresh kit project:
Load http://localhost:3000 twice.
Expected behavior
Cookie header should appear in server console on second load.
Information about your SvelteKit Installation:
Severity
Blocking for any project that requires credentialed server-side requests.
The text was updated successfully, but these errors were encountered: