I'm working on a Sails application, all while teaching myself about Sails and Node. I am following this tutorial, and though I had to make a few changes here and there due to bcrypt's windows requirements being absolutely insane, I got the bulk of it working. My issue seems to be in the tutorials config/passport.js
file. I've ended up modifying the contents a bit as I was going through debug.
The exact error is
C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.res.setHeader (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\node_modules\connect\lib\patch.js:134:22)
at ServerResponse.res.set.res.header (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:595:10)
at ServerResponse.res.send (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:151:12)
at ServerResponse.res.json (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:237:15)
at ServerResponse.res.send (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\express\lib\response.js:139:21)
at C:\Users\Jhecht\Desktop\sails\students\api\controllers\AuthController.js:22:30
at C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\http\request.js:51:48
at pass (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\authenticator.js:287:14)
at Authenticator.serializeUser (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\authenticator.js:289:5)
at IncomingMessage.req.login.req.logIn (C:\Users\Jhecht\Desktop\sails\students\node_modules\sails-auth\node_modules\passport\lib\http\request.js:50:29)
at C:\Users\Jhecht\Desktop\sails\students\api\controllers\AuthController.js:21:17
at Strategy.strategy.success (C:\Users\Jhecht\Desktop\sails\students\node_modules\passport\lib\middleware\authenticate.js:194:18)
at verified (C:\Users\Jhecht\Desktop\sails\students\node_modules\passport-local\lib\strategy.js:83:10)
at C:\Users\Jhecht\Desktop\sails\students\config\passport.js:28:5
at returnResults (C:\Users\Jhecht\AppData\Roaming\npm\node_modules\sails\node_modules\waterline\lib\waterline\query\finders\basic.js:180:9)
Program exited with code 1
config/passport.js
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bcrypt = require('bcrypt-nodejs');
passport.serializeUser(function (user, done) {
done(null, user.user_id);
});
passport.deserializeUser(function (id, done) {
Users.findOne({
user_id: id
}, function (err, user) {
done(err, user);
});
});
passport.use(new LocalStrategy({
usernameField: 'user_email',
passwordField: 'user_password'
},
function (email, password, done) {
Users.findOne({
user_email: email
}, function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, {
message: 'Email not found, are you sure you registered?'
});
}
if (bcrypt.compareSync(password, user.user_password)) { //Changed from tutorial to see if the error was coming from the async function
console.info("User Found, Hashes are equal."); //Me finding errors
done(null, user, {
message: 'Login Successful'
});
//Removing this line I don't get the error in the console and the server doesn't reset, but without it the authentication doesn't work
}
});
}
));
I unfortunately do not know enough about Sails/Node to figure out what exactly is going on. The error appears to be thrown by the sails-mysql adapter, but only if the done()
function is called. Without the done()
call, however, I can't get Passport to authorize the user.
The tutorial contains an error in AuthController.js
, missing a return
in the login()
function as mentioned in this comment. The req.logIn()
part should be:
req.logIn(user, function(err) {
if (err) return res.send(err);
return res.send({
message: info.message,
user: user
});
});