NodeJS Error: "ERR_HTTP_HEADERS_SENT]: Can not set headers after they are sent to the client"

1

Well, I have a problem with my application, however I still can not understand the reason for this error, since this only happens if a user updates their password and then tries to log in.

// Function validating 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

    
asked by anonymous 21.12.2018 / 22:48

0 answers