title: Node.JS/Passport - Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader
body: <p>I have been trying to perform some authentication with Passport-local and node.js of course. However, I keep on getting the following error:</p>
  
  <pre><code>Error: Can't set headers after they are sent.
      at ServerResponse.OutgoingMessage.setHeader (http.js:689:11)
      at ServerResponse.header (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/express/lib/response.js:717:10)
      at ServerResponse.res.location (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/express/lib/response.js:831:8)
      at ServerResponse.redirect (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/express/lib/response.js:870:8)
      at complete (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/middleware/authenticate.js:241:26)
      at /home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/middleware/authenticate.js:250:15
      at pass (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/authenticator.js:427:14)
      at Authenticator.transformAuthInfo (/home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/authenticator.js:449:5)
      at /home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/middleware/authenticate.js:247:22
      at /home/denny/workspace/nodejs-ecommerce/e-shop/node_modules/passport/lib/http/request.js:51:7
  </code></pre>
  
  <p>My code is as follows:</p>
  
  <pre><code>passport.use(new LocalStrategy({usernameField:'email', passwordField: 'password'},
      function(email, password, done) {
      User.find({ where: { email: email }}).success(function(user) {
            if (!user) {
              done(null, false, { message: 'Unknown user' });
            } 
            else if (password != user.password) {
              done(null, false, { message: 'Invalid password'});
            } 
            else {
              done(null, user);
            }
              }).error(function(err){
            done(err);
          });
          return done(null, false);
      }
  ));
  </code></pre>