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

155
Visualizações
PassportJS Access-Control-Allow-Headers when making Express redirect

I'm receiving the following error:

XMLHttpRequest cannot load http://localhost:4200/#/partners. Request header field content-type is not allowed by Access-Control-Allow-Headers in preflight response.

server.js snippet:

 app.use(function(req, res, next){
     res.header("Access-Control-Allow-Origin", "*");
     res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
     res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
     res.header('Access-Control-Allow-Credentials', "true");
     next();
 });

 .
 .
 .

 app.post("/login", function(req, res, next) {
     passport.authenticate("local", function (err, user, info) {
       if (err) { return next(err); }
       if (!user){ return res.redirect(home_connection+'/#/'); }

       req.logIn(user, function(err){
         if(err){ return next(err); }
         return res.redirect(home_connection+"/#/partners");
       });
     })(req, res, next);
   }
 );

Keep in mind that this error still persists even after I set the Access-Control-Allow-* headers. The path that Express is to redirect to is an Angular2 route, /#/partners.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I used Angular2 to redirect instead of the Node server.

Rather than redirecting to the path within the Node server, I just returned a success confirmation as the response. When Angular2 receives the success confirmation it then routes the app to the proper location.

I modified /login to the following:

app.post("/login", cors(),
  passport.authenticate("local"),
    function(req, res){
      res.status(200).json(req.user);
});

Notice res.status(200).json(req.user); simply returns the user object.

The snippet of Angular2 code that calls /login looks like (keep in mind loginUser is a function in UserService that calls posts to /login):

this.userService
  .loginUser(this.user.phone, this.third.value.code.replace(/ /g, ''))
  .then((resp) => {
    if(resp) this.router.navigate(['/partners']);
    else {
      this.third.setValue({"code":""});
      this.invalidCode = true;
    }
  }).catch((err) => {
    this.invalidCode = true;
  });

Note that this.router.navigate(['/partners']); handles the redirection to /partners.

about 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