Monday, 24 April 2017

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.



via Glenn Dayton

No comments:

Post a Comment