0

I'm using a node.js express back-end with passport-local as the authentication strategy. I'm using react as the front-end, and when I use a fetch request to log in, it works fine. However, the 'Set-Cookie' header in the response doesn't set a cookie in my browser, and when I try to access an authentication protected route on my back-end I get a '401' error.

Login request code (works fine and returns a response with 'Set-Cookie' header):

fetch('http://localhost:3001/login', {
    method: 'post',
    mode: 'cors',
    headers: {
        'content-type': 'application/json',
        'access-control-allow-origin': 'localhost:3000'
    },
    body: JSON.stringify({ email, password })
})

Profile Info Request (doesn't work):

fetch('http://localhost:3001/profile-info', {
    method: 'get',
    mode: 'cors',
    withCredentials: 'true',
    headers: {
        'content-type': 'application/json',
        'access-control-allow-origin': 'http://localhost:3000'
}
})
  • Answered [enter link description here](https://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain) – user3020002 Oct 12 '21 at 22:35

0 Answers0