I have my front-end and back-end both in different domains. I'm trying to make a put request with Axios to authenticate a user using express-session. The call works fine but it seems that the server does not set the cookie in the headers in the response. Here is my code:
This is the code in the front end:
Axios.post('/users/authenticate', {
//DATA
}, {withCredentials: true})
.then((response) => {
console.log(response);
})
.catch((error) =>{
console.log(error);
});
Then my code in the NodeJS server. This is for allowing cors:
app.use(cors({
origin: ['http://localhost:3000'],
credentials: true,
methods: ['POST', 'PUT', 'GET', 'OPTIONS', 'HEAD', 'DELETE'],
exposedHeaders: ["set-cookie"]
}));
Then for setting the cookie:
router.use(session({
secret: 'the secret',
cookie: {maxAge: 2000 * 60, sameSite: 'none', secure: true},
store: store,
resave: true,
saveUninitialized: true,
}));
Finally my response header:
I've tried every solution I found in StackOverflow and other blogs and forums with the options in cors and still not working neither in Chrome, Edge nor Firefox but it does in Postman
I found out the problem. I'm still not sure what's happening in the background.
I had to add this line at the very start of my code:
app.set('trust proxy', 1);
It works now. I can't see the cookie in the application tab in dev tools but I see it in the response headers and is working as expected.
I suppose that there is a proxy between my front-end and back-end. If someone can explain it I will be grateful.