I am attempting to set CORS headers on my API server (myserver.com
) to allow cross-origin, authenticated requests with cookies from my embedded mobile app or other site (localhost:8080
)
Here is the initial request, which appears to be working properly and sets the right cookie.
Summary
URL: https://myserver.com/api/tokens
Status: 200
Source: Network
Address: 1.1.1.1:443
Request
:method: POST
:scheme: https
:authority: myserver.com
:path: /api/tokens
Accept: application/json, text/plain, */*
Content-Type: application/json
Origin: http://localhost:8080
Content-Length: 51
Accept-Language: en-US,en;q=0.9
Host: myserver.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
Referer: http://localhost:8080/
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Response
:status: 200
Content-Type: application/json; charset=utf-8
Access-Control-Allow-Credentials: true
Set-Cookie: token=removedforsecuritypurposes; path=/; expires=Tue, 02 Aug 2022 03:57:50 GMT; samesite=none; secure; httponly
Vary: Origin
Date: Sun, 03 Jul 2022 03:57:50 GMT
Content-Length: 616
Access-Control-Allow-Origin: http://localhost:8080
Server: nginx/1.14.1
I'm using axios
on the frontend, using the withCredentials: true
option to send subsequent requests, but the browser doesn't include the cookie. What am I missing?
Summary
URL: https://myserver.com/api/user
Status: 401
Source: Network
Address: 1.1.1.1:443
Request
:method: GET
:scheme: https
:authority: myserver.com
:path: /api/user
Accept: application/json, text/plain, */*
Origin: http://localhost:8080
Accept-Encoding: gzip, deflate, br
Host: myserver.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
Accept-Language: en-US,en;q=0.9
Referer: http://localhost:8080/
Connection: keep-alive
Response
:status: 401
Content-Type: application/json; charset=utf-8
Access-Control-Allow-Credentials: true
Vary: Origin
Date: Sun, 03 Jul 2022 03:57:50 GMT
Content-Length: 134
Access-Control-Allow-Origin: http://localhost:8080
Server: nginx/1.14.1
I've referenced this post Set cookies for cross origin requests and still can't seem to get it working.