Context
I am writing a SPA in which the frontend and the backend are served from different domains:
mywebsite.com
myapi.com
To keep things simple I am trying to use cookies for authentication. When signing in, the server responds with a set-cookie
header, as shown below:
When making subsequent requests to the API, the cookie should be included to make sure the user is authenticated. For instance:
Problem
This all works well when serving the API on localhost:5000
and the frontend on localhost:4200
, but it doesn't work when deployed. As you can see in the screenshots below, the sign-in
response has the proper set-cookie
header, but the cookie doesn't seem to be set by the browser, and therefore is not sent in future requests.
Response after signing in:
Subsequent request:
Question
Is there a way to make this work with two different domains? Or is it forbidden for security reasons? Would it work if I used subdomains, as in mywebsite.com
and api.mywebsite.com
? Would it work with two subdomains, as in frontend.mywebsite.com
and api.mywebsite.com
?