I'm using fetch to do a request to the backend. The cookie ISN'T set when I use a different domain. The cookie IS set when I use the same domain.
Why is it not being set?
I modified my /etc/hosts file to use pseudonymns to test using the same and different domain, and made sure they are not blacklisted by the browser either.
If I use local-test-frontend.com for both the browser and server domain it works, but if I change the backend url to local-test-backend.com it fails.
*Note that my front end url I test it from is * http://local-test-frontend.com:3000/login
Javascript
fetch('http://local-test-backend.com/login', {
mode: 'cors',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(loginRequest),
credentials: 'include'
}).then(// Other code here.....
Server Response Headers
Access-Control-Allow-Credentials
true
Access-Control-Allow-Origin
http://local-test-frontend.com:3000
Content-Length
103
Content-Type
application/json
Date
Wed, 10 Jul 2019 07:23:49 GMT
Server
Werkzeug/0.15.1 Python/3.7.3
Set-Cookie
MY_TOKEN=a7b8ad50f19…end.com; Path=/; SameSite=Lax
As of 2021 with Edge 90.0.796.0 on Linux, I managed to set CORS cookie with the following approach:
credentials: 'include'. See here for more details.Access-Control-Allow-Origin explicitly set to a domain, could be different from the server domain. For example, in a Single-Page-App architecture, your frontend site is temporarily hosted at localhost:3000 and your backend server hosted at localhost:8000, then the header should be Access-Control-Allow-Origin: http://localhost:3000. See here and here.Access-Control-Allow-Credentials: true. See here. Note that this enforces a non-wildcard setting for Access-Control-Allow-Origin. See here - that's why in point 2 above, it has to be explicitly set to something like http://localhost:3000 rather than *SameSite=None; Secure; HttpOnly. So overall something like Set-Cookie: session_id=12345; SameSite=None; Secure; HttpOnly. SameSite seems to be a relatively new requirement in latest browsers, and must be used with Secure together when SameSite is set to None.HttpOnly, I haven't found relevant materials, but in my experiment, omitting it caused the browser to ignore the Set-Cookie header.credentials: 'include' set.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With