Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda