1
votes

Hello i have this method below and i cant determine why i am getting this error

Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11) at ServerResponse.header (/home/kwanso/Projects/myApp/Server/node_modules/express/lib/response.js:718:10) at ServerResponse.json (/home/kwanso/Projects/myApp/Server/node_modules/express/lib/response.js:246:10) at /home/kwanso/Projects/myApp/Server/app.js:92:11 at Layer.handle [as handle_request] (/home/kwanso/Projects/myApp/Server/node_modules/express/lib/router/layer.js:95:5) at next (/home/kwanso/Projects/myApp/Server/node_modules/express/lib/router/route.js:131:13) at complete (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:243:13) at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:250:15 at pass (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:421:14) at Authenticator.transformAuthInfo (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:443:5) at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:247:22 at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/http/request.js:58:7 at pass (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:267:43) at serialized (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:276:7) at /home/kwanso/Projects/myApp/Server/app.js:97:3 at pass (/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:284:9) -- Called from /home/kwanso/Projects/myApp/Server/node_modules/express/lib/application.js:629:43(function EventEmitter.logerror)

My method is below:

passport.use(new LocalStrategy(
      function(username, password, done) {
        console.log("New Local Strategy");
        User.find({ email: username }, function (err, user) {
          console.log(user[0]._doc);
          if (err) { return done(err); }
          if (!user) { return done(null, false); }
          if (user[0]._doc.password != password) { return done(null, false); }
          console.log("sending");
          return done(null, user);
          console.log("sendt");
        });
        return done(null , false);
      }
    ));

    var isValidPassword = function(user, password){
      return true;
    }

    app.post('/login',
    passport.authenticate('local') ,
    function(req, res) {
      console.log("Back");
          res.send(req.user[0]);
    });

    passport.serializeUser(function(user, done) {
      console.log("Serialize User");
      done(null, user);
    });

    passport.deserializeUser(function(user, done) {
      console.log("De Serialize User");
      done(null, user);
    });

It seems like its something to do with calling res.send in the callback method inside the callback. Is that an issue?

1

1 Answers

4
votes

You invoking done callback inside LocalStrategy Twice

once at the end of function(username, password, done) {

return done(null , false);

and once after fetching User from database

  if (err) { return done(err); }
  if (!user) { return done(null, false); }
  if (user[0]._doc.password != password) { return done(null, false); }
  console.log("sending");
  return done(null, user);

Just remove the last line and you got it

  return done(null , false);