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

295
Visualizações
Is it possible to pass a request header on a link with EJS template? Express/Node.js

I'm a trying to make a route accessible only if you are authenticated with JWT using below middleware but I can't seem to find a way to pass the token in the get request from client side ? It works fine on postman and I if using a fetch from client side it won't redirect me to the page I want to go

auth.js

async function (req, res, next) {
  const token = req.header('x-auth-token');

  if (!token) {
    return res.status(401).json({ msg: 'Forbidden' });
  }

  try {
    const decoded = jwt.verify(token, process.env.TOKEN_SECRET);

    req.user = decoded.user;

    next();
  } catch (e) {
    return res.status(401).json({ err: 'fail' });
  }
};

server side

router.get('/', auth, function (req, res, next) {
  res.render('pages/person');
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can simply attach your token to the headers in the request and sent it with get or even 'delete` method.

for example, in the fetch method you can attach your token in this way in your client side:

fetch('URL_GOES_HERE', { 
   method: 'post', 
   headers: new Headers({
     'Authorization': YOUR_TOKEN, 
     'Content-Type': 'application/x-www-form-urlencoded'
   }), 
 });

Now, you can retrieve the token in the node app:

const token = req.headers.authorization || "";
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