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

213
Visualizações
Headers in react don't persist

Okay so this may be a really junior question but I have just started working with react and I don't know much about it.

The thing is: I'm trying to code an ecommerce. To access a certain page of the website (productos), you need a token (to be logged in, authenticated). So the first thing I do in each page (that requires to be logged in) is to check if the user is logged in, and if it is, I take the token and I set it to the header like this:

function Products()
{
Promise.all([
    fetch('http://localhost:3001/login',
    {
      method: 'get',
      headers: new Headers({
        'Authorization': sessionStorage.getItem("token"),
        'Content-Type': 'application/x-www-form-urlencoded'
      })
    }).then(value => value.json()), 
    fetch('http://localhost:3001/productos').then(value => value.json())
  ]).then((value) => {
    console.log(value)
  })
}

The first fetch is working fine, because it sets the header 'authorization' equals to the token of the user, BUT the second fetch doesn't have this header.

enter image description here

enter image description here

I thought that the header would persist... Since it doesn't, when the fetch of products tries to load the data it doesn't do it, because it doesn't have the header 'authorization'...

Could somebody help me out please?

about 4 years ago · Juan Pablo Isaza
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