Nodejs Error: "ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client"

Asked

Viewed 542 times

1

Good, I have a problem in my application, however I still can not understand well the reason for this error, since this only happens if a user update his password and then try to login.

//Function that validates login

router.post('/processLogin', (req, res, next) => {
  passport.authenticate('login', (err, user, info) => {     
  try {
      if(err || !user){
          res.redirect('/')
      }
      req.login(user, { session : false }, (error) => {
          if( error ) return next(error)
          var token = jwt.sign({ user : user },'pri2018');
          req.session.token = token
          if (user.tipo == "Músico") res.redirect('/musico')
          else if (user.tipo == "Produtor") res.redirect('/produtor')
          else res.redirect('/admin')
      });     
  } 
  catch (error) {
      return next(error);
  }
 })(req, res, next);
});

//Function to update the password

router.put('/:id', function(req, res) {
var form = new formidable.IncomingForm();
console.log(req.user.password)
var oldPass = req.user.password
/* Parses the form */
form.parse(req, (err, fields, files)=>{
  if (!err){
    /* Adds user to Database */
    //Saber se a password foi alterada
    if(oldPass != fields.password){
      fields.password= encryptPassword(fields.password)
    }
    //END
    UserController.updateUser(req.params.id, fields)
    res.end()
  } else {
    res.render("error", {error: err})
  }
})

});

Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.