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

1.3K
Visualizações
Unable to set cookie on a different domain. Express and React

I have my backend hosted on a server, for example, backend.vercel.app and the frontend on another server, for example, frontend.vercel.app

Whenever a user sends a request to the /login route, I am setting the cookie like this:

const setCookie = (req, res, token) => {
  res.cookie('jwt', token, {
    expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000)
  });
};

In the frontend, I am using axios to send a request to the backend.vercel.app/login route, with {withCredentials: true}

But, the cookie is not being set up, even after a successful login.

The cookies are there in the backend API, but not in the frontend: Image of available cookies

What am I missing?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

When you set the cookie on backend.vercel.app for example, the cookie you set is only accessible from that same subdomain, and not accessible by other subdomains. In order for the cookie to be shared, you must set the cookie for the parent domain.

Eg:

res.cookie('jwt', token, {
   expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000),
   domain: "vercel.app"
});

This should work, however if it doesn't you should check you cors configuration.

over 4 years ago · Santiago Trujillo Relatório

0

UPDATE: I fixed this issue by manually specifying the method: 'POST' in the login request. And, used this middleware in the backend:

app.use((req, res, next) => {
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,UPDATE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
  next();
});

I also applied the CORS configuration, thanks to the comment made by Kenneth Lew.

Cheers!

over 4 years ago · Santiago Trujillo 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