Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
Cookie not being set in headers using CORS with NodeJS

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:

enter image description here

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!