Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda